将多个路由分配给ASP MVC 6中的同一控制器或操作 [英] Assigning multiple routes to the same controller or action in ASP MVC 6

查看:110
本文介绍了将多个路由分配给ASP MVC 6中的同一控制器或操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

在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.

推荐答案

在新的RC1应用程序中,在控制器级别具有2个路由属性可以正常工作:

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/loghttp://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之前(关于此行为的一篇不错的文章

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/loghttp://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中的同一控制器或操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆