I noticed something weird in my MVC project. I actually went back to the vanilla version to try it out and found out that the following (last list item being my only addition to the project, except for the action Test in the controller, only returning an empty instance of View) works well.
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("Test", "Test", "Home")</li>
</ul>
However, when I added style to the action link as follows, I get an error. It works still but now VS remarks and when I check the remark, it asks me if I want to create a new controller Shared and an action Test.
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("Test", "Test", "Home", new { @style = "color: blue;" })</li>
</ul>
Why is it so? Is it a real problem at all?
There are a finite number of overloads to that method. Your "working" version is likely resolving to this one, which is explicitly referencing the "Home" controller as expected:
ActionLink("Test", "Test", "Home")
However, your "not working" version seems like it would reasonably resolve to this one, which "works" for the HTML attributes but changes what "Home"
means in this case, treating it as a route value (which probably isn't parsed to anything useful and is ignored):
ActionLink("Test", "Test", "Home", new { @style = "color: blue;" })
Does it really generate the correct route in the client-side link? If so, I'd consider that a lucky coincidence but wouldn't rely on it.
You're probably looking for this overload, which would look like this:
ActionLink("Test", "Test", "Home", null, new { @style = "color: blue;" })