asp.net mvc Html.ActionLink() keeping route value I don't want

若如初见. 提交于 2019-11-26 11:00:45

问题


I have the following ActionLink in my view

<%= Html.ActionLink(\"LinkText\", \"Action\", \"Controller\"); %>

and it creates the following URL http://mywebsite.com/Controller/Action

Say I add an ID at the end like so: http://mywebsite.com/Controller/Action/53 and navigate to the page. On this page I have the markup I specified above. Now when I look at the URL it creates it looks like this:

http://mywebsite.com/Controller/Action/53 (notice the addition of the ID)

But I want it to remove the ID and look like it did originally, like this http://mywebsite.com/Controller/Action (notice no ID here)

Any ideas how I can fix this? I don\'t want to use hard coded URLs since my controller/actions may change.


回答1:


The solution is to specify my own route values (the third parameter below)

<%= Html.ActionLink("LinkText", "Action", "Controller", 
    new { id=string.Empty }, null) %>



回答2:


It sounds like you need to register a second "Action Only" route and use Html.RouteLink(). First register a route like this in you application start up:

routes.MapRoute("ActionOnly", "{controller}/{action}", 
   new { controller = "Home", action = "Index" } );

Then instead of ActionLink to create those links use:

Html.RouteLink("About","ActionOnly")



回答3:


The problem is the built in methods take input from the URL you are currently on as well as what you supply. You could try this:

<%= Html.ActionLink("LinkText", "Action", "Controller", new { id = ""}) %>

That should manually wipe the id parameter.




回答4:


Don't know why, but it didn't work for me (maybe because of Mvc2 RC). Created urlhelper method =>

 public static string
            WithoutRouteValues(this UrlHelper helper, ActionResult action,params string[] routeValues)
        {
            var rv = helper.RequestContext.RouteData.Values;
            var ignoredValues = rv.Where(x=>routeValues.Any(z => z == x.Key)).ToList();
            foreach (var ignoredValue in ignoredValues)
                rv.Remove(ignoredValue.Key);
            var res = helper.Action(action);
            foreach (var ignoredValue in ignoredValues)
                rv.Add(ignoredValue.Key, ignoredValue.Value);
            return res;
        }



回答5:


If you either don't know what values need to be explicitly overridden or you just want to avoid the extra list of parameters you can use an extension method like the below.

<a href="@Url.Isolate(u => u.Action("View", "Person"))">View</a>

The implementation details are in this blog post




回答6:


I explicitly set the action name as "Action/". Seems a little like a hack but it's a quick fix.

@Html.ActionLink("Link Name", "Action/", "Controller")



回答7:


Another way is to use ActionLink(HtmlHelper, String, String, RouteValueDictionary) overload, then there are no need to put null in the last parameter

<%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>



回答8:


The overloads of Html.ActionLink are changed on the later versions of MVC. On MVC 5 and above. This is how to do this:

@Html.ActionLink("LinkText", "Action", "Controller", new { id = "" }, null)

Note I passed "" for id parameter and null for HTMLATTRIBUTES.




回答9:


I needed my menu links to be dynamic. Rather than implement a lot of extra code and routing for every single page I simple dispensed with the HTML helper.

<a href="@(item.websiteBaseURL)/@(item.controller)/@(item.ViewName)">@item.MenuItemName</a>


来源:https://stackoverflow.com/questions/780643/asp-net-mvc-html-actionlink-keeping-route-value-i-dont-want

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!