Routes for IIS Classic and Integrated Mode

     Introduction:

 

          ASP.NET MVC Routing feature makes it very easy to provide clean URLs. You just need to configure routes in global.asax file to create an application with clean URLs. In most cases you define routes works in IIS 6, IIS 7 (or IIS 7.5) Classic and Integrated mode. But in some cases your routes may only works in IIS 7 Integrated mode, like in the case of using extension less URLs in IIS 6 without a wildcard extension map. So in this article I will show you how to create different routes which works in IIS 6 and IIS 7 Classic and Integrated mode.

 

    Description:

 

          Let's say that you need to create an application which must work both in Classic and Integrated mode. Also you have no control to setup a wildcard extension map in IIS. So you need to create two routes. One with extension less URL for Integrated mode and one with a URL with an extension for Classic Mode.

 

01routes.MapRoute(
02    "DefaultClassic", // Route name
03    "{controller}.aspx/{action}/{id}", // URL with parameters
04    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
05);
06 
07routes.MapRoute(
08    "DefaultIntegrated", // Route name
09    "{controller}/{action}/{id}", // URL with parameters
10    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
11);

 

 

          Now you have set up two routes, one for Integrated mode and one for Classic mode. Now you only need to ensure that Integrated mode route should only match if the application is running in Integrated mode and Classic mode route should only match if the application is running in Classic mode. For making this work you need to create two custom constraint for Integrated and Classic mode. So replace the above routes with these routes,

 

 

01routes.MapRoute(
02    "DefaultClassic", // Route name
03    "{controller}.aspx/{action}/{id}", // URL with parameters
04    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
05    new { mode = new ClassicModeConstraint() }// Constraints
06);
07 
08routes.MapRoute(
09    "DefaultIntegrated", // Route name
10    "{controller}/{action}/{id}", // URL with parameters
11    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
12    new { mode = new IntegratedModeConstraint() }// Constraints
13);

 

         The first route which is for Classic mode adds a ClassicModeConstraint and second route which is for Integrated mode adds a IntegratedModeConstraint. Next you need to add the implementation of these constraint classes.

 

 

01public class ClassicModeConstraint : IRouteConstraint
02{
03    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
04    {
05        return !HttpRuntime.UsingIntegratedPipeline;
06    }
07}
08 
09public class IntegratedModeConstraint : IRouteConstraint
10{
11    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
12    {
13        return HttpRuntime.UsingIntegratedPipeline;
14    }
15}

 

 

         HttpRuntime.UsingIntegratedPipeline returns true if the application is running on Integrated mode; otherwise, it returns false. So routes for Integrated mode only matched when the application is running on Integrated mode and routes for Classic mode only matched when the application is not running on Integrated mode.

 

    Summary:

 

          During developing applications, sometimes developers are not sure that whether this application will be host on IIS 6 or IIS 7 (or IIS 7.5) Integrated mode or Classic mode. So it's a good idea to create separate routes for both Classic and Integrated mode so that your application will use extension less URLs where possible and use URLs with an extension where it is not possible to use extension less URLs. In this article I showed you how to create separate routes for IIS Integrated and Classic mode. Hope you will enjoy this article too.

 

2 Comments

Comments have been disabled for this content.