1. 程式人生 > >web api使用JObject接收時,報“無法創建抽象類”錯誤

web api使用JObject接收時,報“無法創建抽象類”錯誤

mic stream lec con end tco return seek https

https://bbs.csdn.net/topics/391952288

在下列函數中增加 ModelBinders.Binders.Add(typeof(JObject), new JObjectModelBinder());

public static void RegisterRoutes(RouteCollection routes)
{
     ModelBinders.Binders.Add(typeof(JObject), new JObjectModelBinder());
}

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); } }

web api使用JObject接收時,報“無法創建抽象類”錯誤