C# 为控制器使用不同的路由模板

C# 为控制器使用不同的路由模板,c#,asp.net-core-mvc,asp.net-core-routing,C#,Asp.net Core Mvc,Asp.net Core Routing,可以在MVC中更改路由控制器名称吗?在MVC 5中,我会这样做: [RoutePrefix("MySpecialSauce")] public class ProductsController : Controller { [Route("GetBy/{id}")] public MyObject GetBy(int id) { return something(id); } } 现在我能找到的就是使用控制器的默认名称: [Route("[con

可以在MVC中更改路由控制器名称吗?在MVC 5中,我会这样做:

[RoutePrefix("MySpecialSauce")]
public class ProductsController : Controller
{
    [Route("GetBy/{id}")]
    public MyObject GetBy(int id)
    {
        return something(id);
    }
}
现在我能找到的就是使用控制器的默认名称:

[Route("[controller]")]
public class ProductsController : Controller
{

    [HttpGet("GetBy/{id}")]
    public MyObject GetBy(int id)
    {
        return something(id);
    }
}

我想为我的路线使用与实际控制器名称不同的名称。如何做到这一点?

在Core中也可以做到这一点

[Route("MySpecialSauce")]
public class ProductsController : Controller {

    [HttpGet("GetBy/{id:int}")]//Matches GET MySpecialSauce/GetBy/5
    public MyObject GetBy(int id) {
        return something(id);
    }
}
[controller]
是一种代币替代品,有助于使用路由模板。这不是强制性的

来源

为方便起见,属性路由支持通过 将令牌括在方括号中(
[
]
)。令牌
[action]
[area]
[controller]
将替换为 操作的操作名、区域名和控制器名 路线已定义