Does the ASP.NET main route tag helper use Route?

I am confused about how the asp-route- * tag helper works. I understand this has something to do with the routing I have installed. For example.

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

      

Here I am displaying my route like this: / Category / PageNumber for the List action in the Product controller

The following code will, when clicked, follow the previous screen

<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 as I understand it, "asp-route-category" will look for "{category}" in my routeMap template, and then "asp-route-page" will look for "{page}" in the routeMap template?

The MS documentation is kind of confusing or just abstract, can anyone confirm or explain this better?

+3


source to share


1 answer


When using normal routing , the way you do it, the parameters controller

and are required action

and are mapped to your controller and action name.

So, if you want to navigate to Action List

in Controller Product

using Category and Page parameter, your routing should look like this:

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

      

Update:



The attribute asp-route

on Tag Helpers that you are using will provide parameters for routing values.

Basically, it asp-route-MyParameter

will add MyParameter

for routing values ​​with the specified value.

More information here .

+3


source







All Articles