更改 Asp.net 核心路由

时间:2021-05-20 10:04:33

标签: asp.net asp.net-core

如何将控制器的默认地址更改为所需的地址?例如将 http://Home/main?id=2 更改为 http://Home/main/my_text

1 个答案:

答案 0 :(得分:0)

如果您使用的是属性路由,那么下面将是解决方案。

public class HomeController: BaseController
{
    [Route("/Home")]
    [Route("/Home/Main")]
    [Route("/Home/Main/{value}")]
    public IActionResult Main(string value = "my_text")
    {
        // ...
    }
}

以上操作将针对以下路线执行,您将获得此值。

<头>
路线 价值
/首页 my_text
/首页/主要 my_text
/Home/Main/my_text my_text
/Home/Main/other_text other_text