locked
URL Rewriting Not Working On Published Website RRS feed

  • Question

  • User-1433151636 posted

    I've adapted a simple URL rewriter from Stephen Walther's ASP.NET Unleashed 3.5 book. The technique works fine when running the website from VS 2008, however when published to IIS or my webspace, I get a 404 'not found' error. Can anyone point out where I'm going wrong?

    The relevant parts of the code are a URLMappings.Config file, which remaps any requests to a Region to the Default.aspx page in the Regions folders-

    <urlMappings>
      <add url="~/Regions/.*" mappedUrl="~/Regions/Default.aspx"/>
    </urlMappings>

    This is read by an HTTPModule-

    using System;
    using System.Web;
    using System.Xml;
    using System.Web.Caching;
    using System.Text.RegularExpressions;

    namespace DirectCaravans
    {
        public class UrlRemapper : IHttpModule
        {
            public void Dispose() { }

            public void Init(HttpApplication app)
            {
                app.BeginRequest += new EventHandler(app_BeginRequest);
            }

            public void app_BeginRequest(Object s, EventArgs e)
            {
                HttpApplication app = (HttpApplication)s;
                HttpContext context = app.Context;

                string currentURL = context.Request.AppRelativeCurrentExecutionFilePath.Replace(" ","-");
                XmlDocument urlMappings = GetURLMappings(context);
                XmlNodeList nodes = urlMappings.SelectNodes("//add");

                foreach (XmlNode node in nodes)
                {
                    string url = node.Attributes["url"].Value;
                    string mappedURL = node.Attributes["mappedUrl"].Value;
                    if (Regex.Match(currentURL, url, RegexOptions.IgnoreCase).Success)
                        context.RewritePath(mappedURL);
                }
            }

            private XmlDocument GetURLMappings(HttpContext context)
            {
                XmlDocument urlMappings = (XmlDocument)context.Cache["UrlMappings"];

                if (urlMappings == null)
                {
                    urlMappings = new XmlDocument();
                    string path = context.Server.MapPath("~/UrlMappings.config");
                    urlMappings.Load(path);
                    CacheDependency fileDepend = new CacheDependency(path);
                    context.Cache.Insert("UrlMappings", urlMappings, fileDepend);
                }
                return urlMappings;
            }
        }
    }

    In the Regions/Default.aspx code behind file, the following code gets theregion name by stripping out some extra text, then gets the region ID number and passes it on to a user control, filtering the listing to only show items in that region.

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    string region = Path.GetFileNameWithoutExtension(Request.RawUrl).Replace("-", " ").Replace("Items in ", string.Empty);
                    int regionID = 0;

                    regionID = Region.GetRegionIDFromRegionName(region);
                    CaravanListing1.RegionID = regionID;
                }
            }


    Again, this all works fine when run from VS 2008, but not from my local IIS or webspace. For extra info, the address when running from VS 2008 is 'http://localhost:55102/DirectProject/Regions/Items-in-Scotland' (for example) and in IIS it is 'http://localhost/Regions/Items-in-Scotland'. Any help would be greatly appreciated, as I really can't work out what's going on. 

    Saturday, December 5, 2009 3:22 PM

Answers

  • User-1433151636 posted

    I've found a solution- it's down to IIS 7, rather than a problem with the code itself. I made changes to the web.config webserver section as suggested [url=http://improve.dk/blog/2009/10/14/how-to-do-url-rewriting-on-iis-7-properly]here[/url], and (bizarrely) had to move the entry for my custom module in the httpmodules section up to the top.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 6, 2009 11:52 AM