public class JsonModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (!IsJSONRequest(controllerContext)) { return base.BindModel(controllerContext, bindingContext); } // Get the JSON data that's been posted var request = controllerContext.HttpContext.Request; var jsonStringData = new StreamReader(request.InputStream).ReadToEnd(); return new JavaScriptSerializer().Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType); } private static bool IsJSONRequest(ControllerContext controllerContext) { var contentType = controllerContext.HttpContext.Request.ContentType; return contentType.Contains("FTchinaMVC/json"); } }
protected void Application_Start() { ModelBinders.Binders.DefaultBinder = new JsonModelBinder(); RegisterRoutes(RouteTable.Routes); } AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters);