簡體   English   中英

ASP.NET Core模型綁定和整數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM