title: ASP.Net C# Routing; HttpHandler; and HttpModule not working
body: <p>I am having quite a few problems with custom extensions and intercepting existing handlers.</p>
  
  <p><strong>What am I trying to do</strong></p>
  
  <p>Based upon persisted options, I would like all 'virtual' extensions to be handled by set handlers.  All pages are dynamically built, and no actual files exist on the site.  The site populates the content, forms the html output and returns it as the web result.
  This is required as I am setting up a fat/thin relationship between 2 servers.  The thin server will simply pass on the request to the fat server - where the request is processed and response issued back down the line.
  The project is for a dynamic multi-domain content management system.  The thin server may not be .net compatible (hence the external request), but will be .net optimised (hence the need for handlers).</p>
  
  <p><strong>The Problem</strong></p>
  
  <p>What I want is to re-route existing extensions - aspx; php; html.
  I have achieved this in my local environment using a custom HttpModule which sets the appropriate handler.  I have explored setting the  tag in config, but the the extensions are re-routed using dynamic rules that are persisted.</p>
  
  <p>As mentioned, this solution works on localhost.
  When uploaded, the .Net extensions are handled by the module correctly but any custom extensions or non-.net extensions return a 404 error.</p>
  
  <p>Seeking an alternative, I have experimented with routing within Global, but this dis not work either.</p>
  
  <p>I have also attempted to use  to register the custom extensions... but each are met with the same result - 404 not found.</p>
  
  <hr>
  
  <h2>Global Routing attempt:</h2>
  
  <pre><code>public class Global : System.Web.HttpApplication
  {
  
      void Application_Start(object sender, EventArgs e)
      {
          RegisterRoutes(RouteTable.Routes);
      }
  
      public static void RegisterRoutes(RouteCollection routes)
      {
  
          routes.Add(new Route("{action}.sqs", new SqlRequestHandler()));
      }
  </code></pre>
  
  <hr>
  
  <h2>.Config (for handler and module attempt)</h2>
  
  <pre><code>    &lt;system.web&gt;
        &lt;compilation debug="true" targetFramework="4.0" /&gt;
        &lt;httpRuntime requestValidationMode="2.0" /&gt;
        &lt;customErrors mode="Off"/&gt;
  
        &lt;httpHandlers&gt;
          &lt;add path="*.sqs" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" /&gt;
          &lt;add path="*.sql" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" /&gt;
        &lt;/httpHandlers&gt;
  
        &lt;httpModules&gt;
          &lt;add name="SisBerCMS" type="CmsMapper.VirtualModule, CmsMapper" /&gt;
        &lt;/httpModules&gt;
    &lt;/system.web&gt;
  
    &lt;system.webServer&gt;   
        &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
        &lt;modules&gt;
          &lt;add name="SisBerCMS" type="CmsMapper.VirtualModule, CmsMapper" /&gt;
        &lt;/modules&gt;
  
        &lt;handlers&gt;
          &lt;add path="*.sqs" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" name="sqsHandler" /&gt;
          &lt;add path="*.sql" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" name="sqlHandler" /&gt;
        &lt;/handlers&gt;
    &lt;/system.webServer&gt;
  </code></pre>
  
  <hr>
  
  <h2>Custom Module (CmsMapper.VirtualModule)</h2>
  
  <pre><code>    if (extentionMap != null)
      {
          // note that extentionMap.ExtentionType is a predetermined enum
          switch (extentionMap.ExtentionType)
          {
              // If the extention is banned then pass back a generic message
              case ExtentionType.Banned:
                  this.WriteTextResponce("Invalid extention detected:" + extentionMap.Extention);
                  break;
  
              // Remap .Ajax requests to the ajax handler
              case ExtentionType.Ajax:
                  this._app.Context.RemapHandler(new AjaxHandler());
                  break;
  
              // Remap session query or sql requests to the sql handler
              case ExtentionType.SessionQuery:
                  this._app.Context.RemapHandler(new SqlRequestHandler());
                  break;
  
              // if the extention is not ignored, re map to the virtual page handler
              default:
  
                  bool isManagementServer = this._app.Context.Request.Url.Authority != VirtualModule.RESPONSE_SERVER;
                  bool isPostRequest = !String.IsNullOrEmpty(this._app.Context.Request.Form[HtmlRequest.RequestOrigin]);
                  bool isGetRequest = !String.IsNullOrEmpty(this._app.Context.Request.QueryString[HtmlRequest.RequestOrigin]);
                  bool isIgnored = extentionMap.ExtentionType == ExtentionType.Ignore;
  
                  if ((isPostRequest || isGetRequest) &amp;&amp; !isIgnored)
                  {
                      this._app.Context.RemapHandler(new VirtualHandler());
                  }
                  else
                  {
                      this._app.Context.RemapHandler(new ExternalRequestHandler());
                  }
  
                  break;
          }
      }
  </code></pre>
  
  <hr>
  
  <p>All the handlers are pretty standard implementing the following:</p>
  
  <pre><code>public class SqlRequestHandler : IHttpHandler, IRequiresSessionState, IRouteHandler
  </code></pre>
  
  <hr>
  
  <p>Again, the preferred method - HttpModule - works on my localhost machine.  This could be a server config issue (in which case I'm looking for a work around), but the fact that the .net extensions are being handled is strange - as this would imply that issues with medium trust should not apply, however issues regarding extension handling on the server may take priority over the .net application.</p>
  
  <p>The server is shared hosting (therefore I am unable to alter the machine.config files), is IIS6 using 4.0.</p>
  
  <p>Thank you for any suggestions on how to resolve this issue.
  Mike</p>