c# – 在ASP.NET Core中更改控制器路由

所以我有一个HomeController,要访问它以及我必须输入url.com/home/action的动作.

是否有可能将此更改为url.com/anothernamethatpointstohomeactually/action?

解决方法:

我建议你使用属性路由,但当然这取决于你的场景.

[Route("prefix")]
public class Home : Controller {

    [HttpGet("name")]
    public IActionResult Index() {
    }

}

这将在url.com/prefix/name上找到

属性路由有很多选项,一些示例:

[Route("[controller]")] // there are placeholders for common patterns 
                           as [area], [controller], [action], etc.

[HttpGet("")] // empty is valid. url.com/prefix

[Route("")] // empty is valid. url.com/

[HttpGet("/otherprefix/name")] // starting with / won't use the route prefix

[HttpGet("name/{id}")]
public IActionResult Index(int id){ ... // id will bind from route param.

[HttpGet("{id:int:required}")] // you can add some simple matching rules too.

检查Attribute Routing official docs

上一篇:使用变量时,重写nginx上的URI不起作用


下一篇:cisco IOS,nexus和Arista 的vrrp