【问题标题】:Setting the default JSON serializer in ASP.NET MVC在 ASP.NET MVC 中设置默认 JSON 序列化程序
【发布时间】:2013-01-13 13:05:33
【问题描述】:

我正在处理已部分转换为 MVC 的现有应用程序。每当控制器以 JSON ActionResult 响应时,枚举都作为数字而不是字符串名称发送。听起来默认的序列化程序应该是 JSON.Net,它应该将枚举作为它们的名称发送过来,而不是整数表示,但这里不是这种情况。

我是否缺少将其设置为默认序列化程序的 web.config 设置?还是有其他需要更改的设置?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 json.net


    【解决方案1】:

    在 ASP.Net MVC4 中,JsonResult 类中使用的默认 JavaScript 序列化程序仍然是 JavaScriptSerializer(您可以在 code 中查看)

    我认为您将它与 ASP.Net Web.API 混淆了,其中 JSON.Net 是默认的 JS 序列化程序,但 MVC4 不使用它。

    所以你需要配置 JSON.Net 才能使用 MVC4(基本上你需要创建自己的JsonNetResult),关于它的文章很多:

    如果您还想将 JSON.Net 用于控制器操作参数,那么在模型绑定期间,您需要编写自己的 ValueProviderFactory 实现。

    你需要注册你的实现:

    ValueProviderFactories.Factories
        .Remove(ValueProviderFactories.Factories
                                      .OfType<JsonValueProviderFactory>().Single());
    ValueProviderFactories.Factories.Add(new MyJsonValueProviderFactory());
    

    你可以使用内置的JsonValueProviderFactory作为例子或者这篇文章:ASP.NET MVC 3 – Improved JsonValueProviderFactory using Json.Net

    【讨论】:

    • 这有助于发送 JSON 响应,但不利于反序列化传入的 JSON,因为框架试图将传入调用映射到传递给您的操作方法的参数。 . .您如何更改 MVC 为此目的使用的序列化程序?
    • 如果你想使用 Json.NET 作为传入参数,你需要编写自己的 ValueProviderFactory 实现。您可以使用内置的JsonValueProviderFactory 作为示例。您需要使用以下地址注册您的实现:ValueProviderFactories.Factories.Remove( ValueProviderFactories.Factories.OfType&lt;JsonValueProviderFactory&gt;().Single()); ValueProviderFactories.Factories.Add(new MyJsonValueProviderFactory());。另见:dalsoft.co.uk/blog/index.php/2012/01/10/…
    • @Marc 根据源代码:aspnetwebstack.codeplex.com/SourceControl/latest#src/… MVC5 中的JsonResult 仍然使用旧的JavaScriptSerializer使用JSON.net
    • @jpgrassi OfType 是在 System.Linq 命名空间中定义的扩展方法。你的cs文件中有using System.Linq;吗?
    • 郑重声明,MVC 6 终于默认使用了 Json.NET。
    【解决方案2】:

    ASP.NET MVC 5 修复:

    我还没有准备好更改为 Json.NET,就我而言,错误是在请求期间发生的。在我的场景中,最好的方法是修改实际的JsonValueProviderFactory,它将修复应用于全局项目,并且可以通过编辑global.cs 文件来完成。

    JsonValueProviderConfig.Config(ValueProviderFactories.Factories);
    

    添加一个 web.config 条目:

    <add key="aspnet:MaxJsonLength" value="20971520" />
    

    然后创建以下两个类

    public class JsonValueProviderConfig
    {
        public static void Config(ValueProviderFactoryCollection factories)
        {
            var jsonProviderFactory = factories.OfType<JsonValueProviderFactory>().Single();
            factories.Remove(jsonProviderFactory);
            factories.Add(new CustomJsonValueProviderFactory());
        }
    }
    

    这基本上是System.Web.Mvc 中的默认实现的精确副本,但添加了可配置的 web.config appsetting 值aspnet:MaxJsonLength

    public class CustomJsonValueProviderFactory : ValueProviderFactory
    {
    
        /// <summary>Returns a JSON value-provider object for the specified controller context.</summary>
        /// <returns>A JSON value-provider object for the specified controller context.</returns>
        /// <param name="controllerContext">The controller context.</param>
        public override IValueProvider GetValueProvider(ControllerContext controllerContext)
        {
            if (controllerContext == null)
                throw new ArgumentNullException("controllerContext");
    
            object deserializedObject = CustomJsonValueProviderFactory.GetDeserializedObject(controllerContext);
            if (deserializedObject == null)
                return null;
    
            Dictionary<string, object> strs = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            CustomJsonValueProviderFactory.AddToBackingStore(new CustomJsonValueProviderFactory.EntryLimitedDictionary(strs), string.Empty, deserializedObject);
    
            return new DictionaryValueProvider<object>(strs, CultureInfo.CurrentCulture);
        }
    
        private static object GetDeserializedObject(ControllerContext controllerContext)
        {
            if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
                return null;
    
            string fullStreamString = (new StreamReader(controllerContext.HttpContext.Request.InputStream)).ReadToEnd();
            if (string.IsNullOrEmpty(fullStreamString))
                return null;
    
            var serializer = new JavaScriptSerializer()
            {
                MaxJsonLength = CustomJsonValueProviderFactory.GetMaxJsonLength()
            };
            return serializer.DeserializeObject(fullStreamString);
        }
    
        private static void AddToBackingStore(EntryLimitedDictionary backingStore, string prefix, object value)
        {
            IDictionary<string, object> strs = value as IDictionary<string, object>;
            if (strs != null)
            {
                foreach (KeyValuePair<string, object> keyValuePair in strs)
                    CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakePropertyKey(prefix, keyValuePair.Key), keyValuePair.Value);
    
                return;
            }
    
            IList lists = value as IList;
            if (lists == null)
            {
                backingStore.Add(prefix, value);
                return;
            }
    
            for (int i = 0; i < lists.Count; i++)
            {
                CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakeArrayKey(prefix, i), lists[i]);
            }
        }
    
        private class EntryLimitedDictionary
        {
            private static int _maximumDepth;
    
            private readonly IDictionary<string, object> _innerDictionary;
    
            private int _itemCount;
    
            static EntryLimitedDictionary()
            {
                _maximumDepth = CustomJsonValueProviderFactory.GetMaximumDepth();
            }
    
            public EntryLimitedDictionary(IDictionary<string, object> innerDictionary)
            {
                this._innerDictionary = innerDictionary;
            }
    
            public void Add(string key, object value)
            {
                int num = this._itemCount + 1;
                this._itemCount = num;
                if (num > _maximumDepth)
                {
                    throw new InvalidOperationException("The length of the string exceeds the value set on the maxJsonLength property.");
                }
                this._innerDictionary.Add(key, value);
            }
        }
    
        private static string MakeArrayKey(string prefix, int index)
        {
            return string.Concat(prefix, "[", index.ToString(CultureInfo.InvariantCulture), "]");
        }
    
        private static string MakePropertyKey(string prefix, string propertyName)
        {
            if (string.IsNullOrEmpty(prefix))
            {
                return propertyName;
            }
            return string.Concat(prefix, ".", propertyName);
        }
    
        private static int GetMaximumDepth()
        {
            int num;
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            if (appSettings != null)
            {
                string[] values = appSettings.GetValues("aspnet:MaxJsonDeserializerMembers");
                if (values != null && values.Length != 0 && int.TryParse(values[0], out num))
                {
                    return num;
                }
            }
            return 1000;
        }
    
        private static int GetMaxJsonLength()
        {
            int num;
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            if (appSettings != null)
            {
                string[] values = appSettings.GetValues("aspnet:MaxJsonLength");
                if (values != null && values.Length != 0 && int.TryParse(values[0], out num))
                {
                    return num;
                }
            }
            return 1000;
        }
    }
    

    【讨论】:

    • 在我们的应用程序中添加了这两个类并且它起作用了。非常感谢,伙计,您为我们节省了很多时间和麻烦。
    • if (num > _maximumDepth) 中的错误信息有错误。它不是 MaxJsonLength 而是 MaxJsonDeserializerMembers 属性。你的代码救了我。 ;)
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多