Assigning multiple routes to the same controller or action in ASP MVC 6

asp.netasp.net Coreasp.net Core-Mvc

asp.net Problem Overview


Question:

Is there any way to assign two different routes (with parameters) to the same controller in an ASP.NET MVC 6 application?

I Tried:

I tried using multiple route attributes to the controller class and also to the individual actions, did not work.

Notes:

  • I am using ASP.NET Core 1.0 RC1.

  • The reason I want to do this is, I want the api to be compatible with the older version of our mobile app which use the old URL.

Example:

[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) 
    {
        // ...
    }
}

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.

asp.net Solutions


Solution 1 - asp.net

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" });
    }
}

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.

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" });
    }
}

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" });
    }
}

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.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA-SharabianiView Question on Stackoverflow
Solution 1 - asp.netDaniel J.G.View Answer on Stackoverflow