URL Rewriting/Mapping using Global.asax
Ultimately I wound up using a different method, specified in my other blog post on URL Rewriting on GoDaddy and Shared hosting.
However, the method below is actually very useful if you are trying to do certain validation which cannot be expressed in web.config or RegEx prior to redirect, such as checking if querystring is valid and exists in database values, etc.
//try out various request types such as absolute path and raw url to see differences protected void Application_BeginRequest(object sender, EventArgs e) { string originalPath = HttpContext.Current.Request.Path.ToLower(); //HttpContext.Current.Request.RawUrl.ToLower(); //HttpContext.Current.Request.Path.ToLower(); RewritePaths(originalPath); } private void RewritePaths(string originalPath) { Rewrite(originalPath, "default", "index"); Rewrite(originalPath, "home", "index"); Rewrite(originalPath, "index"); Rewrite(originalPath, "login"); } private void Rewrite(string path, string page) { if (path.Contains("/" + page)) { if (!path.Contains(".aspx")) { //Context.RewritePath(path.Replace("/" + page, "/" + page + ".aspx")); Context.RewritePath(page + ".aspx"); } } } private void Rewrite(string path, string frompage, string topage) { if (path.Contains("/" + frompage)) { if (!path.Contains(".aspx")) { //Context.RewritePath(path.Replace("/" + frompage, "/" + topage + ".aspx")); Context.RewritePath(topage + ".aspx"); } } }
Posted on April 12, 2011, in Programming & Development and tagged .net, .net 3.5, asp.net, godaddy, iis7, mapping, rewriting, url, url mapping, url rewriting. Bookmark the permalink. 1 Comment.
Pingback: URL Rewriting on GoDaddy and Shared hosting « Fraction of the Blogosphere