小编Dmi*_*tri的帖子

不要将特定属性绑定到ASP.NET WebApi中的模型

我有在数据库中创建新电子邮件的操作。代码就像:

[ValidateModel]
public HttpResponseMessage Post(string username, [FromBody] EmailDTO email)
    ...
    this.repository.Add(username, email)
Run Code Online (Sandbox Code Playgroud)

但是API用户可以设置'Id'属性,这会导致数据库冲突。绑定时无需创建自定义模型绑定程序或更改DTO的构造函数,我是否可以忽略“ Id”属性?像Bind(Exclude ='...'),但是在WebApi中?

有什么已知的方法可以防止在控制器动作中进行大规模分配?

asp.net asp.net-web-api

5
推荐指数
1
解决办法
880
查看次数

标签 统计

asp.net ×1

asp.net-web-api ×1