Whats the differences between QueryString
in Request
and RouteData.Values
?
Can we use them instead ?
Generally, the query string is one of client-side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc. In asp.net mvc routing has support for query strings in RouteConfig.cs let’s have a look on.
Here we will learn query string parameters in asp.net mvc with example. Generally, the query string is one of client-side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc.
Entries in RouteValues can override entries in RouteValues. Represents a case-insensitive collection of key/value pairs that you use in various places in the routing framework, such as when you define the default values for a route or when you generate a URL that is based on a route. Learn about model validation in ASP.NET Core MVC and Razor Pages.
endpoints.MapControllerRoute ("parameters", "parameters/ {level}/ {type}/ {id}", defaults: new { controller = "Home", action = "Parameters" }); But the "postTitle" value is part of the query string and therefore not part of the route data. To access route data in MVC views, we can use ViewContext.RouteData.Values:
RouteValues are gathered from querystring only if are defined in global.asax, for example:
routes.MapRoute(
"Example", // Route name
"{controller}/{action}/{id}/{inRouteValues}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
will catch inRouteValues from yourdomain/testController/testAction/14/myTestValue
where RouteData.Values["inRouteValues"]
will be string with value "myTestValue".
But if you will build URL like yourdomain/testController/testAction/14?inRouteValues=myTestValue
it won't get it. So difference is that RouteData.Values
will get only values from URLs that match RouteCollection
from your global.asax and QueryString
will catch every value from your querystring if it matches variable name.
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