Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambient values in mvc2.net routing

I have following two routes registered in my global.asax file

routes.MapRoute(
    "strict",
    "{controller}.mvc/{docid}/{action}/{id}",
    new { action = "Index", id = "", docid = "" },
    new { docid = @"\d+"}
);
routes.MapRoute(
    "default",
    "{controller}.mvc/{action}/{id}",
    new { action = "Index", id = "" },
    new { docConstraint = new DocumentConstraint() }
);

and I have a static "dashboard" link in my tabstrip and some other links that are constructed from values in db here is the code

 <ul id="globalnav" class = "t-reset t-tabstrip-items">
     <li class="bar" id = "dashboard">
         <%=Html.ActionLink("dash.board", "Index", pck.Controller,  new{docid =string.Empty,id = pck.PkgID }, new { @class = "here" })%>
     </li>
     <%  
         foreach (var md in pck.sysModules)
         {
     %>
     <li class="<%=liClass%>">
         <%=Html.ActionLink(md.ModuleName, md.ActionName, pck.Controller, new { docid = md.DocumentID}, new { @class = cls })%>
     </li>
     <%
         }
     %>
 </ul>

Now my launching address is localhost/oa.mvc/index/11 clearly matching the 2nd route. But when I visit any page that has mapped to first route and then come back to dash.board link it shows me localhost/oa.mvc/7/index/11 where 7 is docid and picked from previous Url.

I understand that my action method is after docid and changing it would not clear the docid.

My question here is, can I remove docid in this scenario without changing the route?

like image 353
Muhammad Adeel Zahid Avatar asked Aug 13 '10 10:08

Muhammad Adeel Zahid


2 Answers

I have the same "not clearing out" value problem...

I've stepped into source code and I don't understand the reason for being of segment commented as : // Add all current values that aren't in the URL at all

@ System\Web\Routing\ParsedRoute.cs, public BoundUrl Bind(RouteValueDictionary currentValues, RouteValueDictionary values, RouteValueDictionary defaultValues, RouteValueDictionary constraints) method from line 91 to line 100

While the clearing process is correctly handled in method preceding steps, this code "reinjects" the undesired parameter into acceptedValues dictionary!?

like image 107
Mathias Kozlowski Avatar answered Sep 28 '22 03:09

Mathias Kozlowski


My routing is defined this way:

routes.MapRoute(
    "Planning",
    "Plans/{plan}/{controller}/{action}/{identifier}",
    new { controller = "General", action = "Planning", identifier = UrlParameter.Optional },
    new { plan = @"^\d+$" }
);

// default application route
routes.MapRoute(
    "Default",
    "{controller}/{action}/{identifier}",
    new {
        controller = "General",
        action = "Summary",
        identifier = UrlParameter.Optional,
        plan = string.Empty // mind this default !!!
    }
);

This is very similar to what you're using. But mind my default route where I define defaults. Even though my default route doesn't define plan route value I still set it to string.Empty. So whenever I use Html.ActionLink() or Url.Action() and I want plan to be removed from the URL I call it the usual way:

Url.Action("Action", "Controller", new { plan = string.Empty });

And plan is not included in the URL query string any more. Try it out yourself it may work as well.

like image 29
Robert Koritnik Avatar answered Sep 28 '22 03:09

Robert Koritnik