Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 749533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:26:45+00:00 2026-05-14T14:26:45+00:00

I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink()

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:26:45+00:00Added an answer on May 14, 2026 at 2:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that this question has been asked 100times but none that I have
I'm pretty sure the question has been asked and answered, I don't know HTML
I have a big question that has been puzzling me for a long time
I realise this question has been asked many times before and I've tried pretty
I have a class called Question that has a property called Type. Based on
I have a UIView subclass ( CustomView for purposes of this question) that has
Fairly simple question: I have an init method on my class that has the
Similar to this question I have a custom subclass of UITableViewCell that has a
I have a rather theoretical question. My company has a working standard (documented) that
sorry if this question has been asked before, I searched but wasn't sure on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.