3

I read a lot of blogs and questions available regarding the routes in asp.net core but did not find any one mentioning something similar.

In some conditions I want my routes to look like:

/products/1/reviews/1

Where products and reviews are followed by their id OR more nested route like:

/products/1/images/1/comments/1

Is there a way I can define a template route for it like the default one:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});
Tseng
  • 61,549
  • 15
  • 193
  • 205
  • 1
    @user3378165: Please use the correct tags if you edit the posts. The question is about asp.net-core and asp.net-core-mvc. not asp.net-mvc which is the tag for the legacy ASP.NET MVC Framework (MVC1-5) – Tseng Jun 27 '16 at 08:02
  • @Tseng, thank you for your comment would you be able to explain me what is the difference between them? – user3378165 Jun 27 '16 at 09:33
  • 2
    @user3378165: https://docs.asp.net/en/latest/conceptual-overview/aspnet.html In Short: It's a complete new framework that's not compatible with the legacy ASP.NET and ASP.NET MVC – Tseng Jun 27 '16 at 11:30
  • @Tseng Thank you very much for the explanation. – user3378165 Jun 28 '16 at 08:55

3 Answers3

3

You can use Attribute based routing

[Route("[conroller]/[action]")]
public class ImagesController
{
    [HttpGet("/[controller]/{productId:int}/images/{imageId:int}/comments/{commentId:int}")]
    public IActionResult GetComments(int productId, int imageId, int commentId) 
    {
    }
}
Tseng
  • 61,549
  • 15
  • 193
  • 205
  • 1
    The OP has asked for a solution to patch his leaky ship. You have prescribed the solution "just use a dinghy". Sure, you *could* use attribute routing, but it is not a solution to the problem at hand, it just moves it to a different forum. – NightOwl888 Jun 27 '16 at 07:28
  • It's the recommended way of doing routing in ASP.NET Core. There are several issues with the "default route mapping", especially when using both controller and action parameters within the route or optional parameters. What attribute based routing does is replace the [controller] and [action] tokens with the actual names when discovering and registering the routes, like if you had it hardcoded in like `products/{productId:int}/comments/{id:int}`, just that gets updated when you rename the controller/action – Tseng Jun 27 '16 at 08:00
  • 1
    There are no "issues" with convention based routing, other than most people struggle to understand how it works. The fact of the matter is, attribute routing in the end just uses the same `IRouter` interface that all routes use. Attribute routing is just a *limited subset* of what convention-based routing can do that happens to be more convenient for some people, but too limited in scope for others. Prescribing attribute routing as a "fix" because of a lack of understanding of convention-based routing is just wrong. – NightOwl888 Jun 27 '16 at 09:31
  • 1
    There are issues, they may be called corner cases though. i.e. if you want have an optional locale for all of your routes, i.e. `{locale}/{controller}/{action}` and `{controller}/{action}` it doesn't work well with default routes, but works perfectly well with attributes on i.e. a base controller `[Route("{locale}/[controller]/[action]")][Route("[controller]/[action]")]`, because when the routes are registered via attributes the `{locale}/[controller]/[action]` gets evaluated and results in `{locale}/Products/Index` for example. With routes, you'd need to register every controller+action – Tseng Jun 28 '16 at 10:16
  • 1
    And scroll to the bottom of the official documentation's https://docs.asp.net/en/latest/fundamentals/routing.html page on routing and you'll see that Attribute based routing is the recommended way of doing routing in ASP.NET Core – Tseng Jun 28 '16 at 10:17
  • Thank you for the detailed Explantation – Segmentation Fault Aug 07 '16 at 08:26
2
routes.MapRoute(
    name: "Products",
    template: "products/{productId}/{action?}/{commentId?}",
    defaults: new { controller = "Products", action = "Index" });

https://docs.asp.net/en/latest/fundamentals/routing.html - Routing in ASP.NET Core

Konstantin Tarkus
  • 37,618
  • 14
  • 135
  • 121
1

There is no difference with the route format of ASP.NET and ASP.NET Core.

You can check this one: ASP.NET Routing

To achieve your goal, all you have to do is define the route ON TOP of your default route.

DevEstacion
  • 1,947
  • 1
  • 14
  • 28