简体   繁体   English

global.asax中的ASP.NET动态URL重写

[英]ASP.NET Dynamic URL REWRITING in global.asax

i need to implement asp.net url rewriting using regular expression in global.asax in one line for this solution 对于此解决方案,我需要在一行中使用global.asax中的正则表达式来实现asp.net网址重写

www.dummydomain.com/a/1/b/2/c/3/d/4/...                    
www.dummydomain.com/b/2/c/3
www.dummydomain.com/b/2/a/1/c/3/

it means changing the parameter sequence should not affect + number of distinct params will be dynamic + i can access these parameters value by name eg a, b, c 这意味着更改参数顺序不会影响+不同参数的数量将是动态的+我可以按名称访问这些参数值,例如a,b,c

You can configure URL rewriting rules via web.config and also programatically. 您可以通过web.config以及以编程方式配置URL重写规则。

Look at following MSDN article, it explains it in depth 请看下面的MSDN文章,它对它进行了深入的解释

http://msdn.microsoft.com/en-us/library/ms972974.aspx http://msdn.microsoft.com/en-us/library/ms972974.aspx

Also in short words to rewrite URLs programarically call HttpContext.RewritePath(string path) from Application_BeginRequest() in global.asax.cs 同样用简短的词来重写URL,以编程方式从global.asax.cs中的Application_BeginRequest()调用HttpContext.RewritePath(字符串路径)

You can use the split option for strings and then put your values into a dictionary. 您可以对字符串使用split选项,然后将值放入字典中。 This assumes that your pattern will never break. 假设您的模式永远不会中断。

Dim MyContext = HttpContext.Current
Dim url = Request.Path.ToLower()
url = url.Trim("/")
Dim Vals = url.Split("/")
Dim Dict As New Dictionary(Of String, String)
for i = 0 to (vals.count/2)-1
     Dict.Add(vals(i),vals(i+1))
next

You can then use the dictionary object to locate your variables. 然后,您可以使用字典对象查找变量。

Run this in your global.asax file in the Application_BeginRequest method and call MyContext.RewritePath to send the user to the correct path. 在Application_BeginRequest方法的global.asax文件中运行此命令,然后调用MyContext.RewritePath将用户发送到正确的路径。

if dict("a") = 1 then MyContext.RewritePath("new URL")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM