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.

 

            routes.MapRoute(
                "DefaultClassic", // Route name
                "{controller}.aspx/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "DefaultIntegrated", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

 

 

          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,

 

 

            routes.MapRoute(
                "DefaultClassic", // Route name
                "{controller}.aspx/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { mode = new ClassicModeConstraint() }// Constraints
            );

            routes.MapRoute(
                "DefaultIntegrated", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { mode = new IntegratedModeConstraint() }// Constraints
            );

 

         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.

 

 

    public class ClassicModeConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return !HttpRuntime.UsingIntegratedPipeline;
        }
    }

    public class IntegratedModeConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return HttpRuntime.UsingIntegratedPipeline;
        }
    }

 

 

         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.

 

Published Friday, December 17, 2010 1:15 PM by imran_ku07
Filed under: , ,

Comments

# Twitter Trackbacks for Routes for IIS Classic and Integrated Mode - Imran Baloch's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Routes for IIS Classic and Integrated Mode - Imran Baloch's Blog         [asp.net]        on Topsy.com

# re: Routes for IIS Classic and Integrated Mode

Wednesday, December 22, 2010 4:27 AM by bandzie

Does it mean we do not need anymore to manually setup wildcard map during asp mvc deploy on IIS 6.0 (and below) environments running in the classic mode? Thank you in advance

# re: Routes for IIS Classic and Integrated Mode

Wednesday, December 22, 2010 6:10 AM by imran_ku07

@bandzie,

No, it simply means when you move from IIS 7 to IIS 6 or vice versa, then you do not need to change the routes.

# re: Routes for IIS Classic and Integrated Mode

Friday, December 24, 2010 3:52 AM by Jameel Ahmed

Nice one