如何在ASP.NET路由中使用查询字符串?
-
03-07-2019 - |
题
新的ASP.NET路由非常适合简单路径样式的URL,但是如果你想使用如下的URL:
http://example.com/items/search ?.xhtml术语=文本+至+找到&安培;页= 2个
您是否必须使用catch all参数进行验证?
解决方案
路线中未列出的任何视图数据项都会自动映射到查询字符串,因此如果您映射“items / search.xhtml”,采取行动:
Search(string term, int page)
然后你应该得到你想要的结果。
其他提示
如果您想捕获添加参数所需的所有内容,也可以将查询字符串参数与路径匹配:
{*}的contentURL
将把剩下的url填充到该变量中。
我也无法将编码后的URL作为路由参数传递给路由。
您不能在URL中使用url编码的字符,但可以在查询字符串中使用。
因此我需要我的路由也有一个查询字符串元素。
说我有路线:
MapPageRoute("myroute", "myroute/{x}", "~/routehander.aspx")
但我希望它的形式为:
http://mywebsite.com/myroute/{x}?url=myurl
我们可以这样做:
Dim x as integer = 12
Dim rvd As New Routing.RouteValueDictionary
rvd.Add("x", x)
rvd.Add("url", Server.UrlEncode("/default.aspx"))
HttpContext.Current.ApplicationInstance.Response.RedirectToRoutePermanent("myroute", rvd)
这会将我们重定向到以下网址:
http://mywebsite.com/myroute/12?url=%252fdefault.aspx
您仍然可以使用 Request.QueryString [" some_value"];
不隶属于 StackOverflow