A project was recently upgraded from ASP.net MVC 3 to just released ASP.net MVC 5.1, and an AJAX request was found to have 500 errors, with detailed exception information logged in the log as follows:
System.ArgumentException: An item with the same key has been added. (an item with the same key has already been added) in System.Collections.Generic.Dictionary ' 2.Insert (TKey key, TValue VA Lue, Boolean Add) in System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore (Entrylimiteddictionary Backingstore, String prefix, Object value) in System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore (entrylimiteddictionary Backingstore, String prefix, Object value) in System.Web.Mvc.JsonValueProviderFactory.GetValueProvider ( ControllerContext controllercontext) in System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider ( ControllerContext controllercontext) in System.Web.Mvc.ControllerBase.get_ValueProvider () at System.Web.Mvc.Controlle Ractioninvoker.getparametervalue (ControllerContext controllercontext, Parameterdescriptor ParameterDescriptor) in System.Web.Mvc.ControllerActionInvoker.GetParameterValues (ControllerContext ControllerContext, Actiondescriptor Actiondescriptor) in System.Web.Mvc.Async.AsyNccontrolleractioninvoker.<>c__displayclass21.<begininvokeaction>b__19 (AsyncCallback AsyncCallback, Object asyncstate) in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase ' 1.Begin (AsyncCallback callback , object state, Int32 timeout) in System.web.mvc.async.asyncresultwrapper.begin[tresult] (AsyncCallback callback, Object State, Begininvokedelegate begindelegate, endinvokedelegate ' 1 enddelegate, Object tag, Int32 timeout) in SYSTEM.WEB.MVC . Async.AsyncControllerActionInvoker.BeginInvokeAction (ControllerContext controllercontext, String ActionName, AsyncCallback callback, Object State)
Although the problem is due to the upgrade to MVC 5.1 caused, but the spirit of "encounter problems, first doubt yourself" principle, check the code, unexpectedly in the JS code found a long-standing low-level error:
var Pagingbuider = {"PageIndex": 1};
function buildpaging (pageIndex) {
pagingbuider.pageindex = PageIndex;
$.ajax ({
data:JSON.stringify (pagingbuider),
contentType: ' Application/json; Charset=utf-8 '
});
PageIndex in the assignment of a pageindex (the 1th letter capital P written in lowercase p), in JS, the beginning of the letter lowercase is also the canonical writing, then may be intuitive to write out, so this low-level error understandable.
/* At this point you may not be tempted to ask: Why do you find yourself, the beginning of the letter in uppercase? Hey, I also have my difficulties, this JS code is generated in the server based on the properties of C # objects, C # is the specification of the initial letter capital * *
Because of such a low-level error, the JSON string sent to the server at the time of the AJAX request becomes this:
{"PageIndex": 1, "PageIndex": 2}
At this time, the vigor of picking up, a big question mark appeared in front of ...