1
votes

I have the following routes:

routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(name: "out", template: "outbound/{controller=Home}/{action=Index}/{id?}");

Why is this not allowed? This outgoing URL is generated from HomeController and action method = Index

<a asp-route="out" asp-action="a" asp-route-id="someIdValue">This is an outoing URL.</a>

The view that generates this outgoing URL is from the HomeController's Index method.

Why wouldn't it use the "out" route template and just use "a" for the action, and using Home as the controller to generate /outbound/Home/a/someIdValue?

I'm able to provide a value for id, why can't I provide a value for the action?

1
Could you add the link that is actually generated? Or do you get an error?janw
@janw yes, /outbound/Home/A works because I have a HomeController and an action method called a()David Klempfner

1 Answers

0
votes

You can use asp-area rather than asp-route.

enter image description here

Startup.cs

    app.UseMvc(routes =>
        {
            routes.MapRoute(name: "out", template: "{area=Outbound}/{controller=Home}/{action=Index}");

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

Controller

[Area("Outbound")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

}



    <a href="/outbound/home/index">Go to Outoing’ Home Page by href="/outbound/home/index"</a> <br>

    <a asp-area="Outbound" asp-controller="Home" asp-action="Index">Go to Outoing’ Home Page by asp-area</a><br>

    @Html.ActionLink("Go to Outoing’ Home Page by @Html.ActionLink", "Index", "Home", new { area = "Outbound" })

Screenshots of test

enter image description here

asp-route

The asp-route attribute is used for creating a URL linking directly to a named route. Using routing attributes, a route can be named as shown in the SpeakerController and used in its Evaluations action:

[Route("/Speaker/Evaluations", 
       Name = "speakerevals")]
public IActionResult Evaluations() => View();

In the following markup, the asp-route attribute references the named route:

<a asp-route="speakerevals">Speaker Evaluations</a>

The Anchor Tag Helper generates a route directly to that controller action using the URL /Speaker/Evaluations. The generated HTML:

<a href="/Speaker/Evaluations">Speaker Evaluations</a>

If asp-controller or asp-action is specified in addition to asp-route, the route generated may not be what you expect.

To avoid a route conflict, asp-route shouldn't be used with the asp-controller and asp-action attributes.


BTW, default route and area route automatically assign an order value to their endpoints based on the order they are invoked.





Difference between asp-route and asp-controller & asp-action

Route names(asp-route) can be used to generate a URL based on a specific route.

In the same time, {controller}/{action} also generate URL maps to Home controller and Test action.

    //works
    <a asp-controller="home" asp-action="test">asp-controller="home" asp-action="test"</a><br>

    //works
    <a asp-route="test">asp-route="test"</a><br>

    //InvalidOperationException
    <a asp-route="test" asp-controller="home" asp-action="test">asp-route="test" asp-controller="home" asp-action="test"</a>

InvalidOperationException occurs when set both in one <a> tag.

enter image description here