简体   繁体   中英

ASP.Net C# Routing; HttpHandler; and HttpModule not working

I am having quite a few problems with custom extensions and intercepting existing handlers.

What am I trying to do

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).

The Problem

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.

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.

Seeking an alternative, I have experimented with routing within Global, but this dis not work either.

I have also attempted to use to register the custom extensions... but each are met with the same result - 404 not found.


Global Routing attempt:

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()));
    }

.Config (for handler and module attempt)

    <system.web>
      <compilation debug="true" targetFramework="4.0" />
      <httpRuntime requestValidationMode="2.0" />
      <customErrors mode="Off"/>

      <httpHandlers>
        <add path="*.sqs" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" />
        <add path="*.sql" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" />
      </httpHandlers>

      <httpModules>
        <add name="SisBerCMS" type="CmsMapper.VirtualModule, CmsMapper" />
      </httpModules>
  </system.web>

  <system.webServer>   
      <modules runAllManagedModulesForAllRequests="true" />
      <modules>
        <add name="SisBerCMS" type="CmsMapper.VirtualModule, CmsMapper" />
      </modules>

      <handlers>
        <add path="*.sqs" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" name="sqsHandler" />
        <add path="*.sql" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" name="sqlHandler" />
      </handlers>
  </system.webServer>

Custom Module (CmsMapper.VirtualModule)

    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) && !isIgnored)
                {
                    this._app.Context.RemapHandler(new VirtualHandler());
                }
                else
                {
                    this._app.Context.RemapHandler(new ExternalRequestHandler());
                }

                break;
        }
    }

All the handlers are pretty standard implementing the following:

public class SqlRequestHandler : IHttpHandler, IRequiresSessionState, IRouteHandler

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.

The server is shared hosting (therefore I am unable to alter the machine.config files), is IIS6 using 4.0.

Thank you for any suggestions on how to resolve this issue. Mike

You need to configure web site in IIS 6.0 to route all extensions (including extensionless paths known as wildcard extension mapping ) to ASP.NET ISAPI dll (and disable the check for file exists).

You can of course do this mapping selectively only for those extensions that you want to route via ASP.NET code. But wildcard mapping will be more useful in case you don't have predefined set of extensions.

In the absence of such mappings, IIS will not forward requests for unknown extensions to ASP.NET (and routing code will not even come into picture) - rather IIS will pass the extension to default (static file) handler that will issue 404 if file is not present.

See this article that describes these steps (for ASP.NET MVC but the same applies to your case): http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
Near the end of article, author has given how to add wildcard script map

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM