Reputation: 19
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 e.g. a, b, c
Upvotes: 1
Views: 3201
Reputation: 4270
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")
Upvotes: 0
Reputation: 8091
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
Upvotes: 1