日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

      首頁編程開發(fā)其它知識 → MVC中JSON字符長度超出限制的異常解決方案

      MVC中JSON字符長度超出限制的異常解決方案

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2012/11/30 10:20:08字體大小:A-A+

      作者:西西點擊:0次評論:0次標簽: JSON

      fastjson1.0.0 免費版
      • 類型:編程輔助大。100KB語言:中文 評分:5.0
      • 標簽:
      立即下載

      今日碰到了這么一個異常,異常信息如下:

      Type : System.InvalidOperationException, mscorlib, Version=2.0.0.0,

      Culture=neutral, PublicKeyToken=b77a5c561934e089

      Message : 使用 JSON JavaScriptSerializer 進行序列化或反序列化時出錯。字符串的長度超過了為 maxJsonLength 屬性設(shè)置的值。

      Source : System.Web.Extensions

      Help link : 

      Data : System.Collections.ListDictionaryInternal

      TargetSite : Void Serialize(System.Object, System.Text.StringBuilder,

      SerializationFormat)

      Stack Trace : 在

      System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj,

      StringBuilder output, SerializationFormat serializationFormat)

         在 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object

      obj, SerializationFormat serializationFormat)

         在 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object

      這個異常是在執(zhí)行MVC中的JsonResult的時拋出的,根據(jù)異常的Message得知是序列化的字符串超出了maxJsonLength的限制。并得知這個屬性是由JavaScriptSerializer提供的,因為MVC內(nèi)置的JsonResult是用JavaScriptSerializer進行序列化的。在網(wǎng)上快速搜索了一下,碰到這個問題的童鞋不少,大部分推薦的解決的方法都是在web.config中加入以下配置,設(shè)置maxJsonLength的長度即可。

      <system.web.extensions>
             <scripting>
                 <webServices>
                     <jsonSerialization maxJsonLength="20971520"/>
                 </webServices>
             </scripting>
         </system.web.extensions>

      在自動提示下,很順暢的加上了幾行代碼,以為這是個簡潔的解決方案,但是運行網(wǎng)站之后報以下錯誤:

      分析器錯誤消息: 無法識別的配置節(jié) system.web.extensions。
      這似乎又是碰到了一家人不認識的情況,既然無法識別為什么能帶智能感知的方式輸出,而且是已system.web開頭的,按道理不會識別不出的。以為是拼寫錯誤,經(jīng)過進一步搜索之后,原來是缺乏了聲明,加上對節(jié)點的聲明即可(很大一串的內(nèi)容)。

      <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                        <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                        <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                        <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                        <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    </sectionGroup>
                </sectionGroup>
            </sectionGroup>

       加入了聲明之后,運行正常,但是問題依舊還在,而且不管maxJsonLength設(shè)置成多大都無效,就算改成1個字符,居然還能跑起來。碰到這個問題只能進一步的搜索。在這篇文章中找到了原委http://weblogs.asp.net/rashid/archive/2009/03/23/submitting-my-first-bug-after-asp-net-mvc-1-0-rtm-release.aspx。

      原來MVC框架內(nèi)置的JsonResult代碼中,在使用JavaScriptSerializer時,都是采用的默認值,沒有從maxJsonLength讀取值,即忽略了這個配置。要想使用配置中的值,只能自定義一個JsonResult,重寫原JsonResult的ExecuteResult方法,于是定義一個ConfigurableJsonResult,代碼如下:

      ConfigurableJsonResult

       1  public class ConfigurableJsonResult : JsonResult
       2     {
       3         public override void ExecuteResult(ControllerContext context)
       4         {
       5             if (context == null)
       6             {
       7                 throw new ArgumentNullException("context");
       8             }
       9             if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
      10                 String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
      11             {
      12                 throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
      13             }
      14 
      15             HttpResponseBase response = context.HttpContext.Response;
      16 
      17             if (!String.IsNullOrEmpty(ContentType))
      18             {
      19                 response.ContentType = ContentType;
      20             }
      21             else
      22             {
      23                 response.ContentType = "application/json";
      24             }
      25             if (ContentEncoding != null)
      26             {
      27                 response.ContentEncoding = ContentEncoding;
      28             }
      29             if (Data != null)
      30             {
      31                 JavaScriptSerializer serializer = new JavaScriptSerializer();
      32 
      33                 ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection;
      34            
      35                 if (section != null)
      36                 {
      37                     serializer.MaxJsonLength = section.MaxJsonLength;
      38                     serializer.RecursionLimit = section.RecursionLimit;
      39                 }
      40 
      41                 response.Write(serializer.Serialize(Data));
      42             }
      43         }
      44     }

      關(guān)鍵在紅色標記的代碼,讀取配置的值。JavaScriptSerializer還有其他屬性可配置,沒有列出與實現(xiàn),暫時夠用。

      這樣在返回長字符內(nèi)容的json結(jié)果時,直接替換原JsonResult即可,同時也兼顧了可配置的靈活性。

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)
        推薦文章

        沒有數(shù)據(jù)