Search code examples
c#model-view-controlleriis-7httpmoduleurl-rewriting

IIS7 HTTPModule not able to rewrite path to a MVC sub application


I have an HTTPModule running in IIS 7 providing a number of different URL rewrite services.

For example:

public void Init(HttpApplication context)
{
    ...
    HttpContext.Current.RewritePath(landingPage.NewPath,
                                string.Empty,
                                landingPage.NewQueryString +
                                ((landingPage.NewQueryString == string.Empty) ? "" : "&") +
                                queryString);
    ...
}

I have a sub application with its own app pool written in MVC: http://www.SomeSiteWithURLrewrite.com/SubMVCApplication/

This module works great except for with MVC applications created as sub applications.

When I make an http request that will be rewritten: http://www.SomeSiteWithURLrewrite.com/Arbitrary/Path/To/Be/Rewritten

I receive a HTTP Error 404.0 - Not Found

The 404 error page shows that there was a call to "http://www.SomeSiteWithURLrewrite.com/Arbitrary/Path/To/Be/Rewritten" that had been rewritten to the physical path "C:{RootDirectory}\SubMVCApplication\" with the handler of "StaticFile".

I am not sure why the pipline is not recognizing the "HttpContext.Current.RewritePath" as an MVC request. Is it because the MVC app is in its own application?

How can I call RewritePath (or something similar) and have IIS render an ASP.Net MVC page.in a sub application


Solution

  • If you cross the application boundaries you will need to do a Response.Redirect which issues a client based HTTP 301 redirect command

    ReWritePath is a server only command, and needs access to the routing tables etc. Thus only the current application boundary

    There is no easy answer, other than making a page proxy in each web application to pass through content, but this would be highly inefficient

    e.g. server.com/a/x rewritten to server.com/b/x, you code would have to detect the application boundary changem, and would instead on a redirect would issue an HTTPRequest to get the server.com/b/x content and pass that content back to client? Resource paths etc would become a big issue so probably not a good idea