简体   繁体   中英

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

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

You can configure URL rewriting rules via web.config and also programatically.

Look at following MSDN article, it explains it in depth

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

You can use the split option for strings and then put your values into a dictionary. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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