Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c# - ASP.NET MVC Html.ActionLink Maintains Route Values

I have a question that has pretty much been asked here:

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

However, the final solution is a kludge, pure and simple and I really would like to understand why this happens, if someone can please explain it to me?

For completeness, it is possible to recreate the scenario very easily:

  • Create a new MVC web app.
  • Run it up.
  • Visit the About tab Modify the URL to read /Home/About/Flib - This obviously takes you to the action with an id of 'Flib' which we don't care about.

Notice that the top menu link to About now actually links to /Home/About/Flib - this is wrong as far as I can see, as I now have absolutely no way of using site links to get back to /Home/About

I really don't understand why I should be forced to modify all of my Html.ActionLinks to include new { id = string.Empty } for the routevalues and null for the htmlAttribs. This seems especially out of whack because I already specify id = 0 as part of the route itself.

Hopefully I'm missing a trick here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you look into the source code for the action link you find that

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

will match

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName) {
        return ActionLink(htmlHelper, linkText, actionName, controllerName, new RouteValueDictionary(), new RouteValueDictionary());
    }

Now so far this looks good since it is creating a new route value dictionary so it is not passing along the values in your current context to be added to the new link, which is what will happen.

However, further down in the code where the url is being generated:

public static string GenerateUrl(string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues) {
        if (routeCollection == null) {
            throw new ArgumentNullException("routeCollection");
        }

        if (requestContext == null) {
            throw new ArgumentNullException("requestContext");
        }

        RouteValueDictionary mergedRouteValues = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, requestContext.RouteData.Values, routeValues, includeImplicitMvcValues);

        VirtualPathData vpd = routeCollection.GetVirtualPathForArea(requestContext, routeName, mergedRouteValues);
        if (vpd == null) {
            return null;
        }

        string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);
        return modifiedUrl;
    }

you can see the requestContext being referenced which has access to the routeData and routeCollections, which will contain the id data. When creating the VirtualPathForArea, the following line is where the id value appears in your url:

internal static VirtualPathData GetVirtualPathForArea(this RouteCollection routes, RequestContext requestContext, string name, RouteValueDictionary values, out bool usingAreas) {
        if (routes == null) {
            throw new ArgumentNullException("routes");
        }

        if (!String.IsNullOrEmpty(name)) {
            // the route name is a stronger qualifier than the area name, so just pipe it through
            usingAreas = false;
            return routes.GetVirtualPath(requestContext, name, values);
        }

        string targetArea = null;
        if (values != null) {
            object targetAreaRawValue;
            if (values.TryGetValue("area", out targetAreaRawValue)) {
                targetArea = targetAreaRawValue as string;
            }
            else {
                // set target area to current area
                if (requestContext != null) {
                    targetArea = AreaHelpers.GetAreaName(requestContext.RouteData);
                }
            }
        }

        // need to apply a correction to the RVD if areas are in use
        RouteValueDictionary correctedValues = values;
        RouteCollection filteredRoutes = FilterRouteCollectionByArea(routes, targetArea, out usingAreas);
        if (usingAreas) {
            correctedValues = new RouteValueDictionary(values);
            correctedValues.Remove("area");
        }

        VirtualPathData vpd = filteredRoutes.GetVirtualPath(requestContext, correctedValues);
        return vpd;
    }

The line:

VirtualPathData vpd = filteredRoutes.GetVirtualPath(requestContext, correctedValues);

takes the virtual path (which is just the route) and returns it. So the virtual path would be /Home/About/Flib

Then when that virtual path is returned the following line uses it to set the client url for the action link:

 string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);

So pretty much it looks like the actionlink is set using the virtual path for the area, which is just the route that was matched, which happens to be the default route in this case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...