Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RouteValues vs QueryString MVC?

Whats the differences between QueryString in Request and RouteData.Values ?
Can we use them instead ?

like image 894
Mohammad Dayyan Avatar asked Dec 30 '12 12:12

Mohammad Dayyan


People also ask

What is query string in MVC routing?

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.

What are query string parameters in ASP NET MVC?

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.

What are entries in routevalues in 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.

How to access route data in MVC views?

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:


1 Answers

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 RouteCollectionfrom your global.asax and QueryString will catch every value from your querystring if it matches variable name.

like image 185
Mariusz Avatar answered Oct 03 '22 02:10

Mariusz