Naj*_*aji 5 model-binding asp.net-web-api asp.net-core
您好,当模型到达控制器(Web API)中的操作时,我试图从模型中排除一个属性,
我尝试过[Bind(Exclude ="something")],但似乎它不属于 .net core api
如果您使用的是 ASP.NET Core,请使用Microsoft.AspNetCore.Mvc.ModelBinding.BindNeverAttribute属性 ( [BindNever])。
public class ExampleViewModel
{
// ResponseMessage will not participate in model binding.
[BindNever]
public string ResponseMessage { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
“表示应从模型绑定中排除属性。当应用于属性时,模型绑定系统会排除该属性。当应用于类型时,模型绑定系统会排除该类型定义的所有属性。”
[Bind]如果您收到 json 格式的模型,则该属性不适用于 Web api,请尝试[JsonIgnore]在需要排除的模型属性上使用:
public class MyModel
{
[JsonIgnore]
public string Name { get; set; }
//...
}
Run Code Online (Sandbox Code Playgroud)
行动:
[HttpPost]
public IActionResult Student([FromBody]MyModel model)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7662 次 |
| 最近记录: |