Sha*_*ath 6 c# actionmethod asp.net-core
我看到以下代码应用于 .net core 操作方法:
[HttpPost("MyAction", Name = "MyAction")]
public IActionResult MyAction()
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
HttpPost 属性中“Name”参数的用途是什么?
该Name属性用于Url Generation。和路由无关!几乎所有时候你都可以忽略它。
将以下代码添加到您的控制器中,您将得到“啊哈!”:
[HttpGet("qqq", Name = "xxx")]
public string yyy()
{
return "This is the action yyy";
}
[HttpGet("test")]
public string test()
{
var url = Url.Link("xxx", null); //Mine is https://localhost:44384/api/qqq
return $"The url of Route Name xxx is {url}";
}
Run Code Online (Sandbox Code Playgroud)
第一个操作中的属性Name,例如,用于生成 url 时,仅用于引用该操作yyy。在我的设置中,调用/api/test返回 string The url of Route Name xxx is https://localhost:44384/api/qqq。
Actionyyy可以通过route .../qqq到达,route 是传递给属性构造函数的第一个参数HttpGet。
| 归档时间: |
|
| 查看次数: |
9014 次 |
| 最近记录: |