为控制器使用不同的路由模板
Use different route template for controller
是否可以更改 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]
是用于帮助路由模板的标记替换。这不是强制性的。
来源Token replacement in route templates ([controller], [action], [area])
For convenience, attribute routes support token replacement by
enclosing a token in square-braces ([
, ]
). The tokens [action]
,
[area]
, and [controller]
will be replaced with the values of the
action name, area name, and controller name from the action where the
route is defined.
是否可以更改 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]
是用于帮助路由模板的标记替换。这不是强制性的。
来源Token replacement in route templates ([controller], [action], [area])
For convenience, attribute routes support token replacement by enclosing a token in square-braces (
[
,]
). The tokens[action]
,[area]
, and[controller]
will be replaced with the values of the action name, area name, and controller name from the action where the route is defined.