asp.net设置默认打开页面 Web.config defaultDocument

18 篇文章 0 订阅

Web.config defaultDocument
The web.config file can be used to set a default document, or list of default documents for your website. Web.config can be used to change the default document (page) for an entire site, or on a directory by directory basis. The default page may be a .php, .asp, .htm, .aspx, .cfm, or any other file type handled by the web server.

While some web.config sections require that the containing directory is set as an application, this isn't one of them. A simple web.config with a defaultDocument section may be placed in any directory, and the directory does NOT need to be set as an application. 

Purpose
The default document is returned to the client when the browser requests a directory name (foo.com/, foo.com/somedir/), but doesn't specify a page. If there isn't a default document in the directory, the client will receive a "file not found" or "directory browsing denied" error. The web server is configured to search for default files in the order listed below. If you wish to chage the default document order or add another file name, please follow the directions on this page.
  • index.htm
  • index.html
  • default.asp
  • default.aspx
  • index.asp
  • index.aspx
  • index.cfm
  • index.php
  • default.htm

How it's done

Example default document list. Comments are enclosed in  <!-- -->  and are not required.
  • Clear all default documents, then add a list of documents
    <defaultDocument enabled="true">    <!-- this line enables default documents for a directory -->
       <files>      
           <clear/>     <!-- removes the existing default document list -->                
           <add value="foo.htm"/>     <!-- foo.htm is now the default document  -->                
           <add value="foo.php"/>     <!-- 2nd default document in search order -->                
           <add value="foo.aspx/>     <!-- 3rd default document in search order -->                
           <add value="foo.cfm/>      <!-- 4th default document in search order -->
       </files>
    </defaultDocument>
    

Using Custom Default Documents
  • Use a text editor to create a file named web.config
  • Save the web.config file with the appropriate content
  • Place the web.config file in the directory that you wish to modify

Detailed web.config content
  • If there isn't an existing web.config in the directory, your new web.config should look something like this
    <?xml version="1.0"?>
    <configuration>
       <system.webServer>
          <defaultDocument enabled="true">    <!-- this line enables default documents for a directory -->
             <files>      
                <clear/>     <!-- removes the existing default document list -->                
                <add value="foo.htm"/>     <!-- foo.htm is now the default document  -->                
                <add value="foo.php"/>     <!-- 2nd default document in search order -->                
                <add value="foo.aspx/>     <!-- 3rd default document in search order -->                
                <add value="foo.cfm/>      <!-- 4th default document in search order -->
             </files>
          </defaultDocument>
          <modules runAllManagedModulesForAllRequests="true"/>
       </system.webServer>
    </configuration>
  • If there is an existing web config, without a <system.webServer> section... Your new web.config should look like this
    <?xml version="1.0"?>
    <configuration>
       <system.web>
         .. existing text ..
         .. existing text ..
       </system.web>
       <system.webServer>
          <defaultDocument enabled="true">    <!-- this line enables default documents for a directory -->
             <files>      
                <clear/>     <!-- removes the existing default document list -->                
                <add value="foo.htm"/>     <!-- foo.htm is now the default document  -->                
                <add value="foo.php"/>     <!-- 2nd default document in search order -->                
                <add value="foo.aspx/>     <!-- 3rd default document in search order -->                
                <add value="foo.cfm/>      <!-- 4th default document in search order -->
             </files>
          </defaultDocument>
          <modules runAllManagedModulesForAllRequests="true"/>
       </system.webServer>
    </configuration>

  • If your existing web.config already has a <system.webServer> section, just add the <defaultDocument> section
    <?xml version="1.0"?>
    <configuration>
       <system.web>
         .. existing text ..
         .. existing text ..
       </system.web>
       <system.webServer>
          <defaultDocument enabled="true">    <!-- this line enables default documents for a directory -->
             <files>      
                <clear/>     <!-- removes the existing default document list -->                
                <add value="foo.htm"/>     <!-- foo.htm is now the default document  -->                
                <add value="foo.php"/>     <!-- 2nd default document in search order -->                
                <add value="foo.aspx/>     <!-- 3rd default document in search order -->                
                <add value="foo.cfm/>      <!-- 4th default document in search order -->
             </files>
          </defaultDocument>
          <modules runAllManagedModulesForAllRequests="true"/>
       </system.webServer>
    </configuration>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值