【问题标题】:Controller level parameter with ASP.NET MVC attribute routing带有 ASP.NET MVC 属性路由的控制器级别参数
【发布时间】:2020-01-10 02:02:58
【问题描述】:

有什么方法可以给Controller Routing属性添加参数吗?

类似:

[Route("controller/{id}/"]

public class Controller {
   public Controller(string id) { /*..*/ }

   [HttpGet]
   public ActionResult Get() { /*..*/ }
}

【问题讨论】:

  • 路由参数传入每个action方法,而不是控制器的构造函数。
  • 你到底想达到什么目的?
  • 试图为现有控制器上的所有路由添加一个公共参数

标签: asp.net-mvc


【解决方案1】:

控制器方法参数上的 [FromRoute] 属性应该可以满足您的需求。

示例

[Route("api/Test/{testId}")]
public class TestController: ControllerBase 
{
    [HttpGet]
    [Route("echo")]
    public void TestMethod([FromRoute]string testId)
    {
        return testId;
    }

}

另请参阅this S.O. 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-15
    • 2019-05-10
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多