Get rid of .aspx from virtual path

Topics: Developer Forum
May 2, 2009 at 4:06 AM
Edited May 2, 2009 at 12:21 PM
I have been working tirelessly to try and get url rewriting to work for virtual paths which do not have .aspx to no avail. I added code to Application_AuthorizeRequest in global.asax in order to provide support for url rewriting for this scenario but its not working.

The very strange thing is that this works flawlessly when I run the website from Visual Studio using the cassini web server, but when I publish the site to any other location, including IIS on the dev box, it returns a 404 error.

Maybe I missed something, is there anybody who has gotten this to work? Take a look at my code, I'm a VB guy so maybe the C# isn't as sharp as it could be.

     
        // file=global.asax
        private void Application_AuthorizeRequest(object sender, EventArgs e)
        {
           
            string currentURL = Request.Path.ToLower();
           
            if (currentURL.EndsWith(".aspx")) {
                //checks to see if the file does not exsists.
                if (!System.IO.File.Exists(Server.MapPath(currentURL))) {
                    string queryString = Request.ServerVariables["QUERY_STRING"];
                    string defaultPage = Request.ApplicationPath + "/default.aspx?pg=";
                   
                    string pageId = null;
                    foreach (SiteMapNode node in SiteMap.RootNode.GetAllNodes()) {
                        if (node.Url.ToLower() == currentURL) {
                            pageId = node["pageId"];
                            // TODO: might not be correct. Was : Exit For
                            break; // TODO: might not be correct. Was : Exit For
                        }
                    }
                   
                    if (pageId == null) {
                        pageId = MyWebPagesStarterKit.WebSite.GetInstance().HomepageId;
                    }
                   
                    // Rewrites the path
                    if (queryString != string.Empty) {
                        Context.RewritePath((defaultPage + pageId + "&") + queryString, false);
                    }
                    else {
                       
                        Context.RewritePath(defaultPage + pageId, false);
                    }
                }
            }
            // <--------------  This is my updated code  ------------->
            else if (!currentURL.Contains(".")) {
                string queryString = Request.ServerVariables["QUERY_STRING"];
                string defaultPage = Request.ApplicationPath + "/default.aspx?pg=";
               
                string pageId = null;
                foreach (SiteMapNode node in SiteMap.RootNode.GetAllNodes()) {
                    if (node.Url.ToLower() == currentURL) {
                        pageId = node["pageId"];
                        break;
                    }
                }
               
                if (pageId == null) {
                    pageId = MyWebPagesStarterKit.WebSite.GetInstance().HomepageId;
                }
               
                // Rewrites the path
                if (queryString != string.Empty) {
                    Context.RewritePath((defaultPage + pageId + "&") + queryString, false);
                }
                else {
                   
                    Context.RewritePath(defaultPage + pageId, false);
                }
            }
        }

I also tried using indexOf(".") == -1 instead of .Contains, this worked in cassini as well but alas nothing I've tried has worked outside of Visual Studio.

Any and all help would be greatly appreciated.

Thanks,

Greg
May 3, 2009 at 7:59 AM
Hi Greg. What your trying todo is making a request that doesn't get handled in IIS. There's several tutorials on how to catch all document back to asp_net isapi.
Most people doesn't have control over IIS on their host, but there's another way och doing it.

If you have custom errorpages at your host, you can point all error(404 not found) and point them at a specific .aspx page.
This will catch the request, where you can rewrite(transfer) the page back to your website. Iv'e done this earlier in regular asp with very good result.