异常详细信息: System.MissingMethodException: 无法创建抽象类。

 

asp.net mvc

在使用post向后端传送json数据时报异常,在路由配置中添加如下即可

        public static void RegisterRoutes(RouteCollection routes)
        {
            ModelBinders.Binders.Add(typeof(JObject), new JObjectModelBinder());
            //routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapMvcAttributeRoutes();//使用自定义路由
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );
        }

        public class JObjectModelBinder : IModelBinder
        {
            public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                var stream = controllerContext.RequestContext.HttpContext.Request.InputStream;
                stream.Seek(0, SeekOrigin.Begin);
                string json = new StreamReader(stream).ReadToEnd();
                return JsonConvert.DeserializeObject<dynamic>(json);
            }
        }

 

posted @ 2019-09-24 11:47  JinweiChang  阅读(581)  评论(0编辑  收藏  举报