c# - WebApi:ID之间的不匹配
问题描述
鉴于以下路线
/api/Person/15
我们用 body 对这条路线做一个 PUT:
{
id: 8,
name: 'Joosh'
}
路线段值为 ,15
但[FromBody]
id 为8
。
现在我们的控制器中有如下内容:
public Model Put(string id, [FromBody] Model model)
{
if (id != model.Id)
throw new Exception("Id mismatch!");
// ... Do normal stuff
}
是否有“默认”或 DRY-ish 方法来执行此操作,而不假设它总是像参数 ID 和 Model.Id 属性一样简单?
解决方案
是否有“默认”或 DRY-ish 方法来执行此操作,而不假设它总是像参数 ID 和 Model.Id 属性一样简单?
自定义验证逻辑可以在 ActionFilter 中实现。因为 ActionFilter 是在动作执行中模型绑定之后处理的,所以模型和动作参数可以在 ActionFilter 中使用,而无需从请求正文或 URL 中读取。您可以参考以下工作演示:
自定义验证过滤器
public class ValidationFilter: ActionFilterAttribute { private readonly ILogger _logger; public ValidationFilter(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger("ValidatePayloadTypeFilter"); } public override void OnActionExecuting(ActionExecutingContext context) { var carDto = context.ActionArguments["car"] as Car; var id = context.ActionArguments["id"]; if (Convert.ToInt32(id)!=carDto.Id) { context.HttpContext.Response.StatusCode = 400; context.Result = new ContentResult() { Content = "Id mismatch!" }; return; } base.OnActionExecuting(context); } }
在 ConfigureServices 方法中注册此操作过滤器
services.AddScoped<ValidationFilter>();
将此操作过滤器称为服务
public class Car { public int Id { get; set; } public string CarName { get; set; } } [ServiceFilter(typeof(ValidationFilter))] [HttpPut("{id}")] public Car Put(int id, [FromBody] Car car) { // the stuff you want }
参考:
推荐阅读
- python - 为什么我的多元 LSTM 不断预测零?
- node.js - 如何使用 sequelize(sqlite) 作为数据库在 nodejs(express) 中创建删除方法
- qt - QML中如何分离Repeater的设计和逻辑
- java - 尝试调用虚拟方法 'void androidx.appcompat.widget.ShareActionProvider.setShareIntent(android.content.Intent)
- python-2.7 - kafka-python:使用 0 vs inf secs 超时关闭 kafka 生产者
- python - 如何根据行而不是字符串替换特定数据?Python, 文件
- gnuplot - 我自己的模式类型的 Gnuplot 点大小
- python - python中部分因子设计的DOEpy语法
- postgresql - Postgres 的复制槽是否在跨区域复制中重复?
- c++ - 如何在 MySQL Connector/C++ 8.0 中使用 Table.select()