Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.Net MVC 3 Routing - Generating Outgoing URL using UrlHelper uses request RouteData
    text
    copied!<p>Asp.Net MVC 3, when creating outgoing link for example with UrlHelper will use RouteData from current request. I dont really understand why. </p> <p>Here is my routing </p> <pre><code>routes.MapRoute("car-location", "{car}/{location}/search", new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Search }, new { car = "[a-zA-Z0-9_]+", location = "[a-zA-Z0-9_]+" }); routes.MapRoute("car-only", "{car}/search-car", new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Search }, new { car = "[a-zA-Z0-9_]+" }); </code></pre> <p>Ok, now I try to generate links:</p> <pre><code>@Url.RouteUrl(new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Search, car = "SUV" }) @Url.RouteUrl(new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Search, location = "NY", car = "SUV" }) </code></pre> <p>The result is correct when current URL is /SUV/search-car </p> <ul> <li>/SUV/search-car </li> <li>/SUV/NY/search</li> </ul> <p>and when current url is /SUV/NY/search they both turn into</p> <ul> <li>/SUV/NY/search</li> <li>/SUV/NY/search</li> </ul> <p>So into the first link {location} is transfered from current request. I dont want my links to be changing :) </p> <p>I tried putting empty location</p> <pre><code>$@Url.RouteUrl(new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Search, car = "SUV", location = "" }) </code></pre> <p>so it generates this (uses correct route but adds this as parameter)</p> <p>/SUV/search-car?location=NY</p> <p>Howto generate links that point to the same action and have different set of routing data and be independent of current request url. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload