RouteValues vs QueryString MVC?

房东的猫 提交于 2020-01-02 03:00:47

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14090546/routevalues-vs-querystring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!