[英]ASP.NET Core model binding and integers
在我的Controller
有這個
[HttpGet]
public string Get([FromQuery] QueryObject obj)
{
return "value";
}
public class QueryObject
{
public int Number { get; set; }
}
當我使用此URL調用Get方法時
http:// localhost / MyController ? 數= HELLO
我最終得到一個Number = 0的QueryObject
實例
為什么會這樣? 查詢顯然是一個BAD REQUEST,因為Number
是一個整數,查詢有一個string
。
模型綁定將嘗試解析請求並綁定到對象的屬性。 如果它無效,那么您將在ModelState對象中發現錯誤。
例如
if (!ModelState.IsValid)
{
// The ModelState is a Dictionary
// holding details of the model binding errors
}
有關詳細信息: https : //docs.microsoft.com/en-us/aspnet/core/mvc/models/validation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.