How to Configure IIS 7 to Serve an ASP.NET MVC Application from a Subdirectory as Default Website Root


2 views

When deploying ASP.NET MVC applications in shared hosting environments like DiscountASP.NET, developers often need to serve content from a subdirectory while making it appear as the root. Here's how to properly configure IIS 7 for this scenario.

The most straightforward approach is to create a virtual directory that points to your subfolder:

<configuration>
  <system.webServer>
    <defaultDocument enabled="true">
      <files>
        <add value="Default.aspx" />
      </files>
    </defaultDocument>
    <rewrite>
      <rules>
        <rule name="Root Redirect" stopProcessing="true">
          <match url="^$" />
          <action type="Redirect" url="/sample" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

For a more seamless solution without URL changes, modify your web.config:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="ReverseProxy" stopProcessing="true">
        <match url="^(.*)$" />
        <action type="Rewrite" url="/sample/{R:1}" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

When using URL rewrite, ensure static files are properly served:

<rule name="StaticFiles" stopProcessing="true">
  <match url="\.(css|js|png|jpg|gif)$" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_FILENAME}" matchType="IsFile" />
  </conditions>
  <action type="None" />
</rule>

For ASP.NET MVC applications, you might need additional route configuration:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { 
                controller = "Home", 
                action = "Index", 
                id = UrlParameter.Optional,
                pathPrefix = "sample"
            }
        );
    }
}

When implementing these changes:

  • Clear your browser cache after changes
  • Check IIS logs for 404 errors
  • Use Failed Request Tracing in IIS for detailed troubleshooting
  • Verify file permissions on the subdirectory

If you have full server access, you can modify the physical path directly in IIS Manager:

  1. Open IIS Manager
  2. Select your website
  3. Click "Basic Settings"
  4. Change physical path to point to your subdirectory
  5. Click "Test Settings" to verify permissions

When deploying ASP.NET MVC applications on shared hosting environments like DiscountASP.NET, you often need to serve the application from a subdirectory while making it appear as the root website. The standard approach of accessing the app via www.domain.com/subdir isn't always ideal for production scenarios.

The most elegant solution involves IIS's URL Rewrite Module, which comes pre-installed on most modern hosting environments. Create a web.config file in your root directory with these rules:



  
    
      
        
          
          
          
            
            
          
        
      
    
  

If you have full IIS Manager access, you can configure the virtual directory directly:

  1. Open IIS Manager and navigate to your site
  2. Right-click the site and select "Add Virtual Directory"
  3. Set alias as "/" and physical path to your subdirectory
  4. Convert the virtual directory to an application

When using rewrite rules, you need special handling for static files:



  
  


  
  

After implementation, test these scenarios:

  • Root URL (should serve your MVC app)
  • Direct subdirectory access (should still work)
  • Static file paths (CSS, JS, images)
  • MVC route generation

If you encounter 500 errors or routing problems:



  

This ensures all requests are processed by the MVC routing system.