下划线表格网址编码媒体类型

时间:2015-07-28 07:58:38

标签: c# asp.net asp.net-web-api

将System.Web.Http更新到5.2.3后,我遇到了asp web api的问题。我需要解决下划线模型。在控制器中我使用[FromBody] LoginRequest模型,但是当我使用application / x-www-form-urlencoded时,model为null,application / json工作正常。

全球asax应用启动

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new UnderscoreContractResolver();

GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.FormUrlEncodedFormatter);
GlobalConfiguration.Configuration.Formatters.Insert(0, new UnderscoreFormUrlEncodedMediaTypeFormatter());

UnderscoreFormUrlEncodedMediaTypeFormatter.cs

public class UnderscoreFormUrlEncodedMediaTypeFormatter : FormUrlEncodedMediaTypeFormatter
{
    public override bool CanReadType(Type type)
    {
        if (type == null)
        {
            throw new ArgumentNullException("type");
        }

        return true;
    }

    // This method is a slightly modifie version of JQueryMvcFormUrlEncodedFormatter's implementation.
    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content,
        IFormatterLogger formatterLogger)
    {
        if (type == null)
            throw new ArgumentNullException("type");
        if (readStream == null)
            throw new ArgumentNullException("readStream");
        if (base.CanReadType(type))
            return base.ReadFromStreamAsync(type, readStream, content, formatterLogger);

        // The following expression could probably be better expressed by someone who's saavier with Tasks.
        var readFromStreamAsync = base.ReadFromStreamAsync(typeof(FormDataCollection), readStream, content, formatterLogger);
        readFromStreamAsync = readFromStreamAsync.ContinueWith(
                (obj =>
                {
                    try
                    {
                        var local = RewritePropertyNames((FormDataCollection)obj.Result);
                        return local.ReadAs(type);
                    }
                    catch (Exception ex)
                    {
                        if (formatterLogger == null)
                        {
                            throw;
                        }
                        else
                        {
                            formatterLogger.LogError(string.Empty, ex);
                            return GetDefaultValueForType(type);
                        }
                    }
                }), new CancellationToken());
        return readFromStreamAsync;
    }

    private static string ConvertUnderScoreToCameCase(string input)
    {
        string[] atoms = input.Split('_');
        StringBuilder sb = new StringBuilder();
        foreach (string atom in atoms)
        {
            if (atom.Length > 0)
            {
                sb.Append(char.ToUpper(atom[0]));
            }
            if (atom.Length > 1)
            {
                sb.Append(atom.Substring(1).ToLower());
            }
        }
        return sb.ToString();
    }

    private static FormDataCollection RewritePropertyNames(FormDataCollection formData)
    {
        var keypairs = (from pair in formData
                        let camel = ConvertUnderScoreToCameCase(pair.Key)
                        let camelPropertyName = camel
                        select new KeyValuePair<string, string>(camelPropertyName, pair.Value)
                       ).ToList();

        return new FormDataCollection(keypairs);
    }
}

Ithink问题在(FormDataCollection)obj.Result没有元素,但为什么? - 结果视图展开结果视图将枚举IEnumerable

0 个答案:

没有答案