public
Last active

ASP.NET MVC 3 - Improved JsonValueProviderFactory using Json.Net

  • Download Gist
ExampleModel.cs
C#
1 2 3 4 5 6 7 8 9 10
//Example of a model that won't work with the current JsonValueProviderFactory but will work with JsonDotNetValueProviderFactory
public class CmsViewModel
{
public bool IsVisible { get; set; }
public string Content { get; set; }
public DateTime Modified { get; set; }
public DateTime Created { get; set; }
//This property will not work with the current JsonValueProviderFactory
public dynamic UserDefined { get; set; }
}
Global.asax.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
 
namespace JsonDotNetValueProviderFactoryTestHarness
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
 
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
 
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "TestJsonValueProviderFactory", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
 
}
 
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
 
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
 
//Remove and JsonValueProviderFactory and add JsonDotNetValueProviderFactory
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
}
}
}
JsonDotNetValueProviderFactory.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
using System.Dynamic;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
 
namespace System.Web.Mvc
{
public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
 
var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
var bodyText = reader.ReadToEnd();
 
return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
}
}
}

I have used this but my object "filterList" always turns out to be 0 and when i comment in global.asax works fine but issue throws error: unable to de serialize large JSON object

example:
public JsonResult getSection(List filterList)
{

        var sectionCount = _FilterService.getSectionCount(filterList);
        AttributesToSessionManager(filterList, sectionCount);
        return Json(sectionCount);
    }

I have a large list being posted from client but its not that large that i get this error.
Please help asap!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.