我看到在 .net 核心操作方法中应用了以下代码:
[HttpPost("MyAction", Name = "MyAction")]
public IActionResult MyAction()
{
// some code here
}
HttpPost 属性中“Name”参数的用途是什么?
最佳答案
Name
属性用于 Url Generation 强>。跟路由没关系!您几乎可以一直忽略它。
将以下代码添加到您的 Controller ,您将获得“啊哈!”:
[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}";
}
Name
第一个操作中的属性,例如,当用于生成 url 时,仅用于引用操作 yyy
.在我的设置中,调用 /api/test
返回字符串 The url of Route Name xxx is https://localhost:44384/api/qqq
.
操作 yyy
可以通过路线 .../qqq
到达,传递给 HttpGet
的第一个参数属性构造函数。
关于c# - HttpPostAttribute 中 Name 参数的用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58497798/