asp.net-mvc – WebAPI ModelBinder错误
发布时间:2020-12-16 00:13:21 所属栏目:asp.Net 来源:网络整理
导读:我已经实现了一个ModelBinder但它的BindModel()方法没有被调用,我得到错误代码500,并带有以下消息: 错误: 不能 从’MyModelBinder’创建一个’IModelBinder’.请确保它来源 来自’IModelBinder’并且具有公共无参数 构造函数. 我从IModelBinder派生,并有公
我已经实现了一个ModelBinder但它的BindModel()方法没有被调用,我得到错误代码500,并带有以下消息:
错误: 不能 我从IModelBinder派生,并有公共无参数构造函数. 我的ModelBinder代码: public class MyModelBinder : IModelBinder { public MyModelBinder() { } public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext,ModelBindingContext bindingContext) { // Implementation } } 在Global.asax中添加: protected void Application_Start(object sender,EventArgs e) { ModelBinders.Binders.DefaultBinder = new MyModelBinder(); // ... } WebAPI行动签名: [ActionName("register")] public HttpResponseMessage PostRegister([ModelBinder(BinderType = typeof(MyModelBinder))]User user) { return new HttpResponseMessage(HttpStatusCode.OK); } 用户类: public class User { public List<Communication> Communications { get; set; } } 解决方法
ASP.NET Web API使用与APS.NET MVC完全不同的ModelBinding insfracture.
您正在尝试实现MVC的模型绑定器接口System.Web.Mvc.IModelBinder,但要使用Web API,您需要实现System.Web.Http.ModelBinding.IModelBinder 所以你的实现应该是这样的: public class MyModelBinder : System.Web.Http.ModelBinding.IModelBinder { public MyModelBinder() { } public bool BindModel( System.Web.Http.Controllers.HttpActionContext actionContext,System.Web.Http.ModelBinding.ModelBindingContext bindingContext) { // Implementation } } 进一步阅读: > Parameter Binding in ASP.NET Web API (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 为什么我需要停止使用<%= ...%>来渲染并开始使用Asp.net
- asp.net-mvc – jQuery.Validation.Unobtrusive客户端验证仅
- asp.net-mvc-3 – ASP.NET MVC 3 MSChart错误:此数据系列只
- asp.net – 具有SQL Server成员资格提供程序的Mono?
- asp.net-mvc – 我应该选择云吗?
- .NET 3.5 / VS 2008上的ASP.NET Web Services的自定义HTTP基
- asp.net-core – .net core(csproj)global.json’projects’
- asp.net – 在数据库中存储日期时间?
- asp.net – Webservices可以作为单身人士引起不同用户的问题
- asp.net-core – 在VS 2015 RC中,编译保存不适用于ASP.NET
推荐文章
站长推荐
- asp.net-mvc-3 – AZURE ACS – Windows Live ID
- 共享ASP.NET_SessionId和.ASPXAUTH cookie安全风
- asp.net-mvc – 如何只允许mvc4 razor中的文本框
- asp.net-mvc – 记录死亡的所有黄色屏幕,即使它是
- asp.net – ModalPopupExtender和验证问题
- asp.net c#中的最佳Audit Trail流程?
- asp.net – 为什么我无法访问usercontrol中的页面
- asp.net – 站点之间共享的IIS 7.0应用程序池 –
- asp.net – 麻烦与ajax POST调用WCF服务
- asp.net-mvc-3 – 通过点击提交两次,避免在Asp.n
热点阅读