c# – How to use @Html.ActionLink to link to a multi-threaded route?

Question:

I would like to use @Html.ActionLink to create the following link:

http://localhost:59278/video/categoria/new-movies/3

Since my control is:

[Route("video/categoria/new-movies/{page?}")]
public async Task<ActionResult> Index(int? page)
{

}

I know how to use @Html.ActionLink passing parameters, but in this case the page parameter is part of the route.

Answer:

You are using the Attribute Routing , then remembering only that you should call the method MapMvcAttributeRoutes () your RouteConfig with the following line:

routes.MapMvcAttributeRoutes();

Once that's done, just add the attribute to your controller , just like you're doing.

[Route("video/categoria/new-movies/{page?}")]
public async Task<ActionResult> Index(int? page)
{

}

Even this part I assume you already have, so now let's go to your ActionLink() .

@Html.ActionLink("TEXTO","Index","CONTROLLER",  new { page = 3 }, null)

Remembering that your actionLink "works" as follows:

MvcHtmlString HtmlHelper.ActionLink(
    string linkText, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes
)

Roughly speaking, you first pass the text, then the Action , the Controller , the route values ​​and html attributes.

As you can see, in ActionLink "you don't care" about the routes, just call the Action and the Controller , remembering to pass the necessary parameters.

Scroll to Top