Configuring IIS 7 default document from web.config

December 20, 2007 20:14 by MartinHN

IIS 7 has a lot of changes regarding the configuration model. Using the Integrated Configuration System in IIS 7 you can set configuration of your server on different levels, compared to IIS 6, where the ASP.NET developer were more restricted. Configuring IIS 7 from an ASP.NET Web Application's web.config file works from both ASP.NET 2.0 and ASP.NET 3.5. Note that the server can be restricted to lock certain configuration settings from above the application level, you might encounter this if your website runs in a shared hosting environment.

Setting up the website

To have a test website, I will setup a new website on my Vista machine running IIS 7. I don't want the website to run under the Default Web Site, so first thing, I'll add a hostname to my hosts file. This is located in the drivers\etc folder of your windows directory's system32 folder. Add the following line:

127.0.0.1         iis7test

Open up Internet Information Services (IIS) Manager, and create a new Application Pool. Name it iis7test, and leave the settings as default:

image

Add a new website with the following settings:

image

This should get us started. Open up Visual Studio 2008 (2005 will also work). Open the website you just created inside IIS.

image

The only item in the website is a web.config. Add a new Web Form called Test.aspx.

Right-click the Test.aspx file and click View in browser. You could get an HTTP Error 500.00, like this:

image

To get rid of it, set impersonate to false in the web.config file by adding this line to the system.web section:

<identity impersonate="false" />

Now your site should work fine. Edit the Test.aspx file, so it has some content. Just write Test IIS 7 or something in the HTML mark-up.

If you right-click the website icon in the Solution Explorer, and click View in browser, you will get an error like the one bellow:

image

That is because our website does not have any pages that match the name of the default documents on IIS. If you don't want to be limited by the few default documents that comes with IIS out-of-the-box, you can add your own from web.config by adding these lines:

<system.webServer>
    <defaultDocument>
        <files>
            <clear />
            <add value="Test.aspx" />
        </files>
    </defaultDocument>
</system.webServer>       

 

Maybe the system.webServer section already existed in your web.config file. If so, just add the defaultDocument section. Here you can specify all the filenames that should act as a default document in IIS 7. The <clear /> line is optional. If you want to keep the default settings, and only add your own - just delete that line.

Now when we save the new web.config file, and refresh the browser we se our Test.aspx file:

image

This setting is just one of many that you can specify in your application's web.config file. In the near future I'll be blogging more about specific settings you can use to configure IIS 7 from ASP.NET.

Technorati Tags: ,

kick it on DotNetKicks.com

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Add to: Facebook Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Technorati
Tags: ,
Categories: ASP.NET | IIS7
Actions: E-mail | Permalink | Comments (5) | Comment RSSRSS comment feed

Comments

December 26. 2007 00:02

trackback

Trackback from DotNetKicks.com

Configuring IIS 7 default document from web.config

DotNetKicks.com

December 23. 2008 19:33

emery

Hi,
I am trying to figure out how to in iis 7.0 redirect /default.html to the root / of the site. I want to do this to make the / the cannonical url to reduce duplicate content and consolidate links back to the site. This is the end result that I want: http://www.mysite.com/default.html" rel="nofollow">http://www.mysite.com/default.html will 301 to http://www.mysite.com/

Will what you describe work

emery

January 13. 2009 07:47

busby seo test

Configuring IIS 7 default document from web.config

busby seo test

March 12. 2009 17:28

all funny sayings


I've been reading your blog for quite a while. Thanks for sharing your thoughts. I wish i had time and patience to make a informative post like yours. A ton of information on all the states. Bookmarked your blog.

all funny sayings

March 16. 2009 03:11

how to write good

thanks for this mate!

how to write good

May 28. 2009 09:16

Niskov Fliskov

Thanks for the great article.
I've tried what you said, but I always get the same "Configuration Error" (caused by "<system.webServer>"):
"This configuration section cannot be used at this path. This happens when the site administrator has locked access to this section using <location allowOverride="false"> from an inherited configuration file."

This is a real issue, since we use to deploy the site on many computers, and any configuration of the system will cause a tremendous overhead... any ideas?

Niskov Fliskov

May 28. 2009 12:28

Martin H. Normark

@Niskov

This is because the settings has been locked through the applicationHost.config file located in C:\Windows\System32\inetsrv\config.

As the error message points out, this is due to the allowOverride=false setting. Have a look in the applicationHost.config file and find the defaultDocument configuration element and look for the allowOverride property.

Martin H. Normark

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.0.0
Theme by Mads Kristensen

About the author

Martin Høst Normark

Senior Frontend Developer at TraceWorks.

View Martin Høst Normark's profile on LinkedIn

Xbox 360

Zune

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008