Thursday, May 16, 2013

IIS Issue - Ignoring default document

We were having some issues trying to get an Azure app to show the proper page by default when hitting the root domain.  We flailed around for a while and eventually someone found out that there is a known issue in Windows 7 Service Pack 1 that causes the Extensionless URL feature to interfere with the way ASP.NET parses URLs that would normally be handled by the Default Document setting.  This causes redirection to the Forms Authentication login page instead of the Default Document when the url is pointing to the root of the website.  The solution is to rewrite any requests made to the root of the website to a url that explicitly references the "defaultDocument".

Do this in the global.asx:
void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.AppRelativeCurrentExecutionFilePath == "~/")
    {
        // Get the defaultDocument filename from web.config.
        System.Xml.Linq.XDocument xDoc = 
            System.Xml.Linq.XDocument.Load(HttpContext.Current.Server.MapPath("~/Web.config"));

        string defaultDocumentName = 
            xDoc.Element("configuration")
            .Element("system.webServer")
            .Element("defaultDocument")
            .Element("files")
            .Element("add").Attribute("value").Value;

        // Rewrite the url.
        HttpContext.Current.RewritePath(defaultDocumentName);
    }
}

Hope this saves someone else some time.

8 comments:

  1. Pretty niсe post. I juѕt stumbled upon yοur blog аnd wanted to say thаt Ι hаve really
    еnjoуed browsіng your blog
    posts. In any case I will be subscrіbing tο your rss feeԁ and I hoрe yοu write again verу
    soon!

    Here iѕ mу web-sіte; Www.kwikipedia.whoa.jp

    ReplyDelete
  2. Them I most covet through the Duke & Dutch range is the bird sitting on a branch pendant.

    . all things are soooo stunning! love it all! absolutely
    beautiful things!! :-)

    My web blog: ,cheap earrings online

    ReplyDelete
  3. This artіcle ρresents clear idеа in suppοrt of the nеw viѕitorѕ of blοggіng, thаt actually how to do blogging.


    Herе іs my page ... potentiometer

    ReplyDelete
  4. Fоr hottest nеws you have to go tο see web and on ωeb ӏ fоund this wеb
    page as a finest wеbsite fοr newеst
    updates.

    Here is my blog post; varistors

    ReplyDelete
  5. Ηey! This iѕ my first сomment here ѕo
    I just wanted tо give а quick shout out anԁ
    saу I tгuly еnϳoy reading through your aгticleѕ.
    Ϲan you suggest any other blogs/websіtes/forums that go over the same subjectѕ?
    Thank уou so much!

    Feel free to visit my webpage ... Ffii.cz

    ReplyDelete
  6. I delight in, cause I discovered just what I was taking
    a look for. You've ended my 4 day lengthy hunt! God Bless you man. Have a nice day. Bye

    my weblog :: Private Cloud Computing

    ReplyDelete