c# - HttpPostAttribute 中 Name 参数的用途是什么

标签 c# asp.net-core actionmethod

我看到在 .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/

相关文章:

c# - 我想知道为什么我的处理函数同时运行它的所有函数而不是互相等待?

c# - 路由参数的 XML 注释

razor - 从另一个 TagHelper 调用 TagHelper?

c# - 我应该如何从 Controller 操作 c# asp.net-mvc-2 返回图像?

c# - TimeZoneInfo 在浪漫标准时间需要前一小时不明确

c# - 在一个 LINQ 语句中选择值并向其附加数字?

c# - dotnetcore 和 framework 4.5 之间的兼容性

asp.net-core - .NET Core 托管包

c# - 这个 C# 操作方法代码实际上触发了 301 重定向吗?

asp.net - 使用 Html.Action 调用 [ChildActionOnly] 操作方法时的安全问题