77

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.

2 Answers 2

117

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.

3
  • Thanks, I tried this and works! I added some more information to my example so you can see the problem. Feb 9, 2016 at 18:01
  • 2
    I see now, since the initial part of the urls was different I guess you removed the attribute from the controller and used 2 attributes on the action Feb 10, 2016 at 10:52
  • One thing to note is that you cannot have a "Name" for [Http*, Name =""] when there are multiple routes to an action as you have mentioned in your example '[HttpGet("{id}", Name = "download")]'.
    – Dhanuka777
    Mar 8, 2017 at 0:29
1
 - [RoutePrefix("UrlName")] Public Class Controller: BaseController{
   [Route("{urlKey?}")] public ActionResult Index(string urlKey="") {
   //You can render the view here which has same content return
   View("~/Views/Country/Index.cshtml') } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.