路由值与查询字符串MVC之间有什么区别?

8

Request.QueryStringRouteData.Values有什么区别?它们可以互相替代吗?

1个回答

15

只有在global.asax中定义了RouteValues,才会从查询字符串中收集它们,例如:

routes.MapRoute(
 "Example", // Route name
 "{controller}/{action}/{id}/{inRouteValues}", // URL with parameters
 new { controller = "Home", action = "Index" } // Parameter defaults
 );

将会从yourdomain/testController/testAction/14/myTestValue中捕获inRouteValues, 其中RouteData.Values["inRouteValues"]是字符串,其值为"myTestValue"。
但如果你构建的URL类似于yourdomain/testController/testAction/14?inRouteValues=myTestValue,它就无法获取到它。所以不同之处在于RouteData.Values只会从与global.asax中的RouteCollection匹配的URL中获取值,并且QueryString将捕获查询字符串中与变量名匹配的每个值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接