模型绑定 – WebApi2:自定义参数绑定以绑定部分参数
发布时间:2020-09-21 13:49:46 所属栏目:asp.Net 来源:互联网
导读:我有一个webApi2项目和另一个项目,其中我有我的Model类和BaseModel,它是所有模型的基础,如下所示, public class BaseModel{ public string UserId { get; set; }} 所有其他模型都是从我的BaseModel派生的. 在webapi我有我的CustomerController如下, public cl
我有一个webApi2项目和另一个项目,其中我有我的Model类和BaseModel,它是所有模型的基础,如下所示, public class BaseModel { public string UserId { get; set; } } 所有其他模型都是从我的BaseModel派生的. 在webapi我有我的CustomerController如下, public class CustomerController : ApiController { [HttpPost] public GetCustomerResponseModel Get(GetCustomerRequestModel requestModel) { var response = new GetCustomerResponseModel(); //I need only the UserId coming from the BaseModel is binded from request headers var userId = requestModel.UserId; //I want all model data except UserId is binded with default model binding var customerData = requestModel.CustomerData; var someOtherData = requestModel.SomeOtherData; return response; } [HttpPost] public AddStockAlertResponseModel AddStockAlert(AddStockAlertRequestModel requestModel) { var response = new AddStockAlertResponseModel(); //I need only the UserId coming from the BaseModel is binded from request headers var userId = requestModel.UserId; //I want all model data except UserId is binded with default model binding var stockInfo = requestModel.StockInfo; return response; } } 每个发送到CustomerController的请求在请求标头中都有一个“UserId”标头,我需要一个ModelBinder或ParameterBinder或一些功能,它只从请求标头绑定UserId而不触及其他模型参数.我的意思是除了UserId之外的模型参数默认是绑定的. 我不想使用AOP或拦截器或方面..是否可以仅使用asp.net功能绑定UserId,如模型绑定器,参数绑定器等. 解决方法以下是使用HttpParameterBinding的快速示例.在这里,我创建一个自定义参数绑定,我让默认的基于FromBody的绑定使用格式化程序来反序列化请求主体,然后从请求标头获取用户标识并在反序列化对象上设置. (您可能需要在以下代码上添加其他验证检查).config.ParameterBindingRules.Insert(0,(paramDesc) => { if (typeof(BaseModel).IsAssignableFrom(paramDesc.ParameterType)) { return new BaseModelParamBinding(paramDesc); } // any other types,let the default parameter binding handle return null; }); public class BaseModelParamBinding : HttpParameterBinding { HttpParameterBinding _defaultFromBodyBinding; HttpParameterDescriptor _paramDesc; public BaseModelParamBinding(HttpParameterDescriptor paramDesc) : base(paramDesc) { _paramDesc = paramDesc; _defaultFromBodyBinding = new FromBodyAttribute().GetBinding(paramDesc); } public override async Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider,HttpActionContext actionContext,CancellationToken cancellationToken) { await _defaultFromBodyBinding.ExecuteBindingAsync(metadataProvider,actionContext,cancellationToken); BaseModel baseModel = actionContext.ActionArguments[_paramDesc.ParameterName] as BaseModel; if (baseModel != null) { baseModel.UserId = actionContext.Request.Headers.GetValues("UserId").First(); } } } (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.net Substitution 页面缓存而部分不缓存的实现方法
- asp.net – 将JSON数据解析为.NET对象的最佳方式
- ASP.NET和System.Diagnostics跟踪 – 我错过了什么,或者这是
- asp.net-mvc-3 – 在ASP.NET MVC 3中覆盖/禁用授权
- ASP.NET MVC4安全性,身份验证和授权
- asp.net – 最后修改标头在MVC
- asp.net – 无法加载文件或程序集App_Licenses
- asp.net-mvc – 在没有模型的情况下手动将验证添加到文本框
- dependency-injection – 从ILogger访问当前的HttpContext
- asp.net – 多个用户控件和JavaScript
推荐文章
站长推荐
- WPF 增加合计一栏
- asp.net 禁用viewstate在web.config里
- asp.net-mvc – 如何锁定ASP.NET MVC中的路径?
- asp.net-mvc – ControllerActionInvoker
- asp.net-mvc – 如何将Model字段值传递给javascr
- ADO.NET 2.0 Dataset和Datatable 新功能新特性
- asp.net-core – 如何使用FluentValidation.AspN
- ASP.NET:从C#代码隐藏显示警报
- asp.net(C#)把汉字转化成全拼音函数(全拼)
- asp-classic – 经典ASP中500 Vs 500.100错误
热点阅读