Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

asp.net - Assigning multiple routes to the same controller or action in ASP MVC 6

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...