问题
When you have an actionlink that is written with no routeValues and you load that page with a routeValue specified MVC writes out the actionlink incorrectly.
I have had exactly the same bug with MVC attribute routing as well where you specify a url something like this: [Route("reviews/{reviewId}")]
I have escpecially had problems with this where it breaks dynamic content pages that are loaded by a routeValue and when you have multiple actionlinks that point to the dynamic action result on the same page.
So here is the example. Can anyone explain why this is happening and how to solve the issue? I feel that it is a bug in MVC as the page really behaves in an unexpected way to me.
Action Result (it defaults to a default content page if no id is provided):
public ActionResult Index(string id)
{
if (id == "" || id == null) {
id = "About-Us";
}
ContentPage cp = LookupContentPage(id);
return View(cp);
}
Action links on a Layout/pages
<ul class="nav navbar-nav">
<li>@Html.ActionLink("About Us", "Index", "Home")</li>
<li>@Html.ActionLink("Terms and Conditions", "Index", "Home", new { id = "Terms and Conditions" }, null)</li>
<li>@Html.ActionLink("User Stories", "Index", "Home", new { id = "User Stories" }, null)</li>
</ul>
Now here is the how to make the bug. On the initial page load the anchor tags load like this:
<ul class="nav navbar-nav">
<li><a href="/">About-Us</a></li>
<li><a href="/Home/Index/Terms%20and%20Conditions">Terms and Conditions</a></li>
<li><a href="/Home/Index/User%20Stories">User Storeies</a></li>
</ul>
This is the result I expect and it implements my default value if not id is provided. However after you navigate to one of the pages that does include an id. So in the case that your URL looks like this: http://localhost:57722/Home/Index/User%20Stories
The nav loads like this:
<ul class="nav navbar-nav">
<li><a href="/Home/Index/User%20Stories">About Us</a></li>
<li><a href="/Home/Index/Terms%20and%20Conditions">Terms and Conditions</a></li>
<li><a href="/Home/Index/User%20Stories">User Stories</a></li>
</ul>
As you can see it has brocken the action link which i have purposely left blank, without an Id so that it should load the default value however it has gone and written its own url which I have not specified in the action link. I feel this is a bug in MVC. Even if you do not have dynamic pages or default with a defualt etc i feel it is brocken that fact that it write the action link incorrectly.
I have solved this issue by creating a seperate ActionResult for the default page if the user leaves the Id blank and then just loading up the dynamic content in another ActionResult which returns the same View.
EDIT 1
I have just added NULL
into the routeValues as was suggested in the comments and it is still breaking:
<li>@Html.ActionLink("About Us", "Index", "Home", null,null)</li>
<li>@Html.ActionLink("Terms and Conditions", "Index", "Home", new { id = "Terms and Conditions" }, null)</li>
<li>@Html.ActionLink("User Stories", "Index", "Home", new { id = "User Stories" }, null)</li>
Out puts like this after navigating to a link with a routeValue
<ul class="nav navbar-nav">
<li><a href="/Home/Index/Terms%20and%20Conditions">About Us</a></li>
<li><a href="/Home/Index/Terms%20and%20Conditions">Terms and Conditions</a></li>
<li><a href="/Home/Index/User%20Stories">User Stories</a></li>
</ul>
回答1:
The route value is added because you using the default route with a segment for the id
value (e.g. url: {controller}/{action}/{id}
). The behavior is by design and all the HtmlHelper
methods that generate a url (e.g. Html.BeginForm()
do the same).
In order to remove the route value, then you can use new { id = "" }
@Html.ActionLink("About Us", "Index", "Home", new { id = "" }, null)
Note that using null
as an argument means use the default value. You could have also generated the same url using
@Html.ActionLink("About Us", null, null, new { id = "" }, null)
来源:https://stackoverflow.com/questions/47401081/actionlink-with-no-routevalues-being-loaded-with-routevalue-breaking-dynamic-pag