TanguyB
TanguyB

Reputation: 2006

ASP.NET Core Route Tag Helper use Route?

I'm kind off confused about how the asp-route-* tag helper works. What I understand is that it's kind of bound to the routing I've setup. E.g.

 routes.MapRoute(
    name: null,
    template: "{category}/Page{page:int}",
    defaults: new { controller = "Product", action = "List" }
 );

Here I map my route as follow: /Category/PageNumber for the Action "List" in Controller "Product"

The following code will, when clicked follow the previous maproute

<a class="btn btn-block 
   @(cat == ViewBag.SelectedCategory ? "btn-primary" : "btn-default")"
    asp-controller="Product"
    asp-action="List"
    asp-route-category="@cat"
    asp-route-page="1">@cat</a>

So how I understand it is that "asp-route-category" will search for "{category}" in my routeMap template, and then "asp-route-page" will search for "{page}" in the routeMap template ?

The documentation on MS is kind off confusing or just to abstract, can someone confirm or explain this in a better way ?

Upvotes: 3

Views: 1905

Answers (1)

S.Dav
S.Dav

Reputation: 2466

When using conventional routing, as you are doing, the controller and action parameters are required and map to your controller and action name.

So if you want to route to the Action List in Controller Product with a Category and a Page parameter, your routing should be like the following:

routes.MapRoute(
    name: null,
    template: "{controller}/{action}/{category}/Page{page:int}",
    defaults: new { controller = "Product", action = "List" }
 );

Update:

The asp-route attribute in the Tag Helpers you are using, will provide parameters to route values.

So basically, asp-route-MyParameter will add MyParameter to route values with the specified value.

More Information here.

Upvotes: 4

Related Questions