问题:
有没有办法在 ASP.NET MVC 6 应用程序中将两个不同的路由(带参数)分配给同一个控制器?
Is there any way to assign two different routes (with parameters) to the same controller in an ASP.NET MVC 6 application?
我试过了:
我尝试对控制器类和单个操作使用多个路由属性,但没有奏效.
I tried using multiple route attributes to the controller class and also to the individual actions, did not work.
注意事项:
我正在使用 ASP.NET Core 1.0 RC1.
I am using ASP.NET Core 1.0 RC1.
我想这样做的原因是,我希望 api 与我们使用旧 URL 的旧版本的移动应用程序兼容.
示例:
[Produces("application/json")]
[Route("api/v2/Log")]
/// The old route is "api/LogFile" which I want to be still valid for this controller.
public class LogController : Controller {
[HttpGet("{id}", Name = "download")]
public IActionResult GetFile([FromRoute] Guid id)
{
// ...
}
}
在上面的示例中:api/LogFile/{some-guid}
是旧路由,api/v2/log/download/{some-guid}
是新路线.我需要两条路线调用相同的操作.
In the example above: api/LogFile/{some-guid}
is the old route and api/v2/log/download/{some-guid}
is the new route. I need both routes invoke the same action.
在控制器级别拥有 2 个路由属性在新的 RC1 应用程序中可以正常工作:
Having 2 route attributes at the controller level works fine in a new RC1 application:
[Produces("application/json")]
[Route("api/[controller]")]
[Route("api/old-log")]
public class LogController: Controller
{
[HttpGet]
public IActionResult GetAll()
{
return Json(new { Foo = "bar" });
}
}
http://localhost:62058/api/log
和 http://localhost:62058/api/old-log
都返回预期的 json.我看到的唯一警告是,您可能需要设置属性的名称/顺序属性,以防您需要为其中一项操作生成 url.
Both http://localhost:62058/api/log
and http://localhost:62058/api/old-log
return the expected json. The only caveat I have seen is that you might want to set the name/order properties of the attributes in case you need to generate the url to one of those actions.
在动作上有 2 个属性也可以:
Having 2 Attributes on the action also works:
[Produces("application/json")]
public class LogController : Controller
{
[Route("api/old-log")]
[Route("api/[controller]")]
[HttpGet]
public IActionResult GetAll()
{
return Json(new { Foo = "bar" });
}
}
但是,在控制器级别拥有通用路由和特定操作路由时需要小心.在这些情况下,控制器级别的路由用作前缀并添加到 url(有一篇关于此行为的好文章 这里).这可能会为您提供一组与您预期不同的 url,例如:
However you need to be careful when having a general route at the controller level and a specific action route. In these cases the route at the controller level is used as a prefix and prepended to the url (There is a nice article about this behavior here). This might get you a different set of urls than you were expecting, for example with:
[Produces("application/json")]
[Route("api/[controller]")]
public class LogController : Controller
{
[Route("api/old-log")]
[Route("")]
[HttpGet]
public IActionResult GetAll()
{
return Json(new { Foo = "bar" });
}
}
在最后一种情况下,您的应用程序将侦听的 2 个路由将是 http://localhost:62058/api/log
和 http://localhost:62058/api/log/api/old-log
因为 api/log
作为前缀添加到在操作级别定义的所有路由.
The 2 routes that your application will listen in the last case will be http://localhost:62058/api/log
and http://localhost:62058/api/log/api/old-log
since api/log
is added as prefix to all the routes defined at the action level.
最后,另一种选择是为新路由使用属性,然后使用启动类中的路由表来提供处理旧 API 的特定路由.
Finally, another option would be to use attributes for your new routes and then use the route table in the startup class to provide specific routes that take care of the old api.
这篇关于在 ASP MVC 6 中将多个路由分配给同一控制器或操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!