Scott
Scott

Reputation: 715

Named Routes with anchor tag helper

I have a default route in my startup.cs Configure method:

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

I navigate to a page with the following url:

http://localhost:59977/Home/Index/somedata

On that above page, I created an anchor tag helper like below:

<a asp-route="default">default route</a>

When I go to view the page on a web browser and look at the anchor element it shows

<a href="/Home/Index/somedata">default route</a>

My question is this. Why has it added the id part (somedata) to the link? I called the route by name and would assume it would use the default options set out in MapRoute, which means id is optional and I did not specify one.

Upvotes: 3

Views: 1451

Answers (1)

Daniel Grim
Daniel Grim

Reputation: 2271

When you use asp-route instead of asp-controller and asp-action, all of the values in RouteData are re-used for your <a> tag. This means that controller is set to "Home", action is set to "Index", and id is set to "somedata" because that was the id field. The default values specified in the route are only used when values do not already exist for those fields.

Upvotes: 2

Related Questions