RouteValues VS查询字符串MVC? [英] RouteValues vs QueryString MVC?
问题描述
什么在请求之间的
和查询字符串
的差异 RouteData.Values
?
我们可以用它们来代替?
Whats the differences between QueryString
in Request
and RouteData.Values
?
Can we use them instead ?
推荐答案
RouteValues,只从查询字符串,如果在Global.asax中的定义,例如云集:
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
);
将捕捉inRouteValues从 YOURDOMAIN /的TestController / testAction / 14 / myTestValue
其中, RouteData.Values [inRouteValues]
将字符串值myTestValue。
但如果你将建立类似的网址 YOURDOMAIN /的TestController / testAction / 14?inRouteValues = myTestValue
也不会得到它。所以,不同的是, RouteData.Values
将匹配 RouteCollection
,从你的Global.asax和<$网址只得到价值C $ C>查询字符串如果匹配的变量名会抓住从你的查询字符串的每一个值。
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.
这篇关于RouteValues VS查询字符串MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!