I'm trying to do something simple in ASP.NET MVC:
RouteValuesDictionary routeValues = GetMyRouteData();
var url = new UrlHelper(Html.ViewContext.RequestContext);
return url.RouteUrl(routeValues);
The problem is that no matter what I do, the url includes route data from the current request context. I want to generate a URL based on ONLY the route values from GetMyRouteData()
.
Thanks
URL routing allows you to configure an application to accept request URLs that do not map to physical files. A request URL is simply the URL a user enters into their browser to find a page on your web site.
MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); The route names give the route a logical name. The named route can be used for URL generation. Using a named route simplifies URL creation when the ordering of routes could make URL generation complicated.
As per our opinion, Attribute Routing provides us more flexibilities as compared to Convention Based Routing. In Attribute Routing, we can manage route as controller level, action level and also area level. Also, it can override the child route, if required.
In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.
The problem is that no matter what I do, the url includes route data from the current request context
That's by design. You have to explicitly set the route values that were present in the original request and that you don't want in the resulting url to null:
var routeValues = GetMyRouteData();
// remove values that you want to exclude from the resulting url
// by setting their values to null
routeValues["id"] = null;
var url = new UrlHelper(Html.ViewContext.RequestContext);
return url.RouteUrl(routeValues);
This is not specific to ASP.NET MVC, but due to ASP.NET Routing's route resolution. The entry point to this is RouteCollection.GetVirtualPath
, which has two signatures.
The first takes a RequestContext
and a RouteValueDictionary
. This is used for default route resolution, which relies on pattern matching to find a route. The route search incorporates all tokens from RequestContext
as well as RouteValueDictionary
; in other words, the two sets of route tokens are combined to form the basis for the route search. A special case exists whereby null parameters in the RouteValueDictionary
remove that parameter from search. However, if such a null-valued parameter has a value in the RequestContext
, that value will still appear in the generated URL as a query string value.
The other signature additionally accepts a route name. It is a little strange because it alters both the route resolution as well as the query string creation. Routes are found, obviously, using name resolution. Given the named route is found, only tokens for those parameters specified in route's URL pattern will appear in the generated URL.
Why is it this way? It's ASP.NET MVC's interpretation of Ruby on Rails' parameter-handling convention.
So default route resolution and "fallback" token resolution are comingled. If you don't want tokens to fallback to the RequestContext
, (and you still want to use ASP.NET Routing) you have to use named route resolution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With