Request routing

Last post 04-02-2008 3:58 PM by bpag. 2 replies.

Sort Posts:

  • Request routing

    03-28-2008, 9:47 AM
    • Contributor
      3,517 point Contributor
    • bpag
    • Member since 04-07-2006, 12:05 PM
    • Columbus, OH
    • Posts 533

    One of the things I like about MVC is that you can have urls like http://server/products/list and you can set up routes to correctly route such a request to your controller and action. I'm wondering if anyone can explain how this works. I realize MVC has an HttpModule and an HttpHandler that get control and use the routes set up to handle requests and route them to the correct place but I don't understand how the HttpModule and Handler even get control. I have written HttpModules in the past and in my experience the only time they get control is if the request is for something that IIS understands to be an ASP.Net item. In other words my HttpModules only gets control if my url contains something.aspx or something.ascx at the end. If I just use a url like I do with my MVC app with no something.aspx or whatever at the end then my HttpModules never get control of the request. Does anyone know how it is that MVC manages to process requests even though they don't contain any file extension at the end that is mapped to an ASP.Net type?

    If this post answered your question please remember to 'Mark as Answer'!
  • Re: Request routing

    03-28-2008, 11:07 AM
    Answer

    bpag:
    how it is that MVC manages to process requests even though they don't contain any file extension

    This is with IIS7 which allows extensionless urls, so with IIS6 you'd typically need to either use .aspx or .mvc if you have mapped these extensions to ASP.NET in IIS, and updated your routes accordingly, unless you use a ISAPI filter with IIS6.

    bpag:
    correctly route such a request to your controller and action

    bpag:
    HttpModule and Handler even get control

    Web.config:

    <add name="UrlRoutingModule" type="System.Web.Mvc.UrlRoutingModule, System.Web.Extensions, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

    You can view the source of the httpmodules on Codeplex:

    http://www.codeplex.com/aspnet/SourceControl/FileView.aspx?itemId=8296&changeSetId=590
    http://www.codeplex.com/aspnet/SourceControl/FileView.aspx?itemId=8295&changeSetId=590

    protected internal virtual void ProcessRequest(HttpContextBase httpContext) {
               // Get the controller type
               string controllerName = RequestContext.RouteData.GetRequiredString("controller");

               // Instantiate the controller and call Execute
               IControllerFactory factory = ControllerBuilder.GetControllerFactory();
               IController controller = factory.CreateController(RequestContext, controllerName);
               if (controller == null) {
                   throw new InvalidOperationException(
                       String.Format(
                           CultureInfo.CurrentUICulture,
                           MvcResources.ControllerBuilder_FactoryReturnedNull,
                           factory.GetType(),
                           controllerName));
               }
               try {
                   ControllerContext controllerContext = new ControllerContext(RequestContext, controller);
                   controller.Execute(controllerContext);
               }
               finally {
                   factory.DisposeController(controller);
               }
           }

  • Re: Request routing

    04-02-2008, 3:58 PM
    • Contributor
      3,517 point Contributor
    • bpag
    • Member since 04-07-2006, 12:05 PM
    • Columbus, OH
    • Posts 533

    Thanks. I didn't realize it was an IIS7 thing. I was running with the studio developer web server and it worked with that so I figured it would work with IIS 6 as well but I guess you're right with II6 you have to have an .aspx or other extention that is mapped to ASP.Net. It makes perfect sense now.

    If this post answered your question please remember to 'Mark as Answer'!
Page 1 of 1 (3 items)