1. 程式人生 > >ASP.NET Core MVC中URL和數據模型的匹配

ASP.NET Core MVC中URL和數據模型的匹配

數據類型 json數據 col user 三個參數 cal spa 匹配 獲取json

Http GET方法


首先我們來看看GET方法的Http請求,URL參數和ASP.NET Core MVC中Controller的Action方法參數匹配情況。

我定義一個UserController,其中有一個只接受GET請求的Action方法GetDataInPage

public class UserController : Controller
{
    [HttpGet]
    public IActionResult GetDataInPage(string languageCode, int currentPage, int pageSize)
    {
        
return View(); } }

GetDataInPage方法有三個參數,那麽接下來我們如果在Http請求的URL中不完全提供這三個參數的值,我們來看看會發生什麽。

首先我們用如下URL,只提供currentPage和pageSize參數的值

http://localhost:9081/User/GetDataInPage?currentPage=5&pageSize=20

我們會發現,UserController的GetDataInPage方法可以成功執行,其中:

  • languageCode參數為null
  • currentPage參數為5
  • pageSize參數為20

這說明如果在URL參數中,缺少languageCode參數是沒有問題的,UserController的GetDataInPage方法可以被執行。

接下來如果我們在如下URL中,只提供languageCode和currentPage參數的值

http://localhost:9081/User/GetDataInPage?languageCode=CN&currentPage=5

我們會發現,UserController的GetDataInPage方法也可以成功執行,其中:

  • languageCode參數為CN
  • currentPage參數為5
  • pageSize參數為0

可以看到即便是我們沒在URL參數中提供pageSize參數,但是pageSize參數在UserController的GetDataInPage方法也被初始化為0了,並沒有妨礙UserController的GetDataInPage方法執行。

接下來如果我們在如下URL中,什麽參數都不提供

http://localhost:9081/User/GetDataInPage

我們會發現,UserController的GetDataInPage方法也可以成功執行,其中:

  • languageCode參數為null
  • currentPage參數為0
  • pageSize參數為0

我們看到這次UserController的GetDataInPage方法還是被執行了,但是其三個參數都沒有從URL中獲取到值,不得不說ASP.NET Core MVC的模型綁定機制包容性還是很強的,這次我們一個URL參數都沒有提供,UserController的GetDataInPage方法還是被執行了。

接下來如果我們在如下URL中,給參數pageSize提供錯誤的數據類型

http://localhost:9081/User/GetDataInPage?languageCode=CN&currentPage=5&pageSize=ABC

可以看到,UserController的GetDataInPage方法要求pageSize參數是int類型,但是我們卻在URL參數中提供的是字符ABC,這種情況下UserController的GetDataInPage方法還是可以被執行,其中:

  • languageCode參數為CN
  • currentPage參數為5
  • pageSize參數為0

可以看到ASP.NET Core MVC直接忽略掉了URL參數中的pageSize=ABC,將UserController中GetDataInPage方法的pageSize參數設置為了0

接下來如果我們在如下URL中,提供多余的URL參數message和number

http://localhost:9081/User/GetDataInPage?languageCode=CN&currentPage=5&pageSize=10&message=ABC&number=100

我們會發現,UserController的GetDataInPage方法也可以成功執行,其中:

  • languageCode參數為CN
  • currentPage參數為5
  • pageSize參數為10

可以看到多余的URL參數message和number,不會對UserController的GetDataInPage方法造成影響,GetDataInPage方法還是被成功執行了。

Http POST方法


接下來,我們來用POST方法的Http請求,看看和ASP.NET Core MVC中Controller的Action方法參數匹配情況。

我們將UserController稍作更改,在其內部定義了一個只接受POST請求的Action方法AddUser,註意我們給AddUser方法的UserModel user參數前面加上了[FromBody]標簽特性,說明UserModel user參數會從Http請求的Body中獲取Json數據來進行模型綁定

public class UserController : Controller
{
    [HttpPost]
    public IActionResult AddUser([FromBody]UserModel user)
    {
        return View();
    }
}

其中AddUser方法的參數UserModel定義如下

public class UserModel
{
    public string UserCode { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string LanguageCode { get; set; }
    public string MailAddress { get; set; }
    public IList<string> RoleCodes { get; set; }
    public int Status { get; set; }
}

然後我們用POST方法的Http請求,發送如下Json數據到,該Json只給UserModel類的

{
  "userCode": "U001",
  "username": "James",
  "status": 1
}

ASP.NET Core MVC中URL和數據模型的匹配