gpt4 book ai didi

c# - RouteValues 与 QueryString MVC?

转载 作者:太空狗 更新时间:2023-10-29 20:03:13 26 4
gpt4 key购买 nike

RequestRouteData.Values 中的 QueryString 有什么区别?
我们可以改用它们吗?

最佳答案

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

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”的字符串。
但是,如果您要构建像 yourdomain/testController/testAction/14?inRouteValues=myTestValue 这样的 URL,它将无法获取。所以不同之处在于,RouteData.Values 只会从与 RouteCollection 匹配的 URL 中获取值,而 QueryString 会从您的 global.asax 中获取每个值查询字符串,如果它匹配变量名。

关于c# - RouteValues 与 QueryString MVC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com