2
をC#では、Iは、列挙データ型を持つキャスト辞書コンテンツを入力する列挙型データ型
public class DataOptions
{
#region Fields
public DebugLevel debug = DebugLevel.NONE;
...
及びデータは、ASいるFormDataにPOSTを介して提供されますINT:
Form Data:
debug: 0
してから、このようなWebAPIの中で解析されます。
[HttpPost]
public AjaxAnswer<SomeDataType[]> MyEndpoint(HttpRequestMessage req) {
DataOptions o = null;
try
{
o = req.DecodeJSON<DataOptions>();
}
catch (Newtonsoft.Json.JsonReaderException) // Not JSON - old frontend versions are sending as form data
{
try {
o = req.DecodeFormData<DataOptions>();
} catch(Exception e) {
return new AjaxAnswer<SomeDataType[]>() {success:false, data:new SomeDataType[0], Error: "Error in MyEndpoint", ErrorMessage: "Could not decode request payload: " + e.Message
}
}
DecodeFormDataはこのようになりますカスタム拡張メソッドです:いくつかの理由について
public static T DecodeFormData<T>(this System.Net.Http.HttpRequestMessage req) where T : new()
{
string postdata = req.Content.ReadAsStringAsync().Result;
Dictionary<string, string> s = postdata.Split('&').Where(x=>x.Contains('=')).Select(x => x.Split('=')).ToDictionary(x => x[0], x => HttpContext.Current.Server.UrlDecode(x[1]));
return s.ToType<T>();
}
、このコードは、エラーがスローされます。
Invalid cast from 'System.String' to 'MyNamespace.DebugLevel'
変換そのenum
実行せずに罰金を通じて。何が起こっているのですか(s.ToType<T>()
の後ろ)、なぜですか?
'postdata'も追加できますか? –
'.ToType'メソッドの由来は? – Frogger