简体   繁体   English

ASP.Net C#路由; HttpHandler; 和HttpModule无法正常工作

[英]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. 该网站填充内容,形成html输出并将其作为Web结果返回。 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). 瘦服务器可能不兼容.net(因此需要外部请求),但是将优化.net(因此需要处理程序)。

The Problem 问题

What I want is to re-route existing extensions - aspx; 我想要的是重新路由现有扩展-aspx; php; php; html. html。 I have achieved this in my local environment using a custom HttpModule which sets the appropriate handler. 我已经在我的本地环境中使用设置适当处理程序的自定义HttpModule实现了此目的。 I have explored setting the tag in config, but the the extensions are re-routed using dynamic rules that are persisted. 我已经探索了在config中设置标签的方法,但是扩展使用持久化的动态规则进行了重新路由。

As mentioned, this solution works on localhost. 如前所述,该解决方案适用于localhost。 When uploaded, the .Net extensions are handled by the module correctly but any custom extensions or non-.net extensions return a 404 error. 上传后,.Net扩展名将由模块正确处理,但是任何自定义扩展名或非.net扩展名均会返回404错误。

Seeking an alternative, I have experimented with routing within Global, but this dis not work either. 寻求替代方法,我已经尝试在Global内进行路由,但这也不起作用。

I have also attempted to use to register the custom extensions... but each are met with the same result - 404 not found. 我还尝试过使用注册自定义扩展名...但是每个都遇到了相同的结果-找不到404。


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) .Config(用于处理程序和模块尝试)

    <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) 自定义模块(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. 再次,首选方法HttpModule在我的本地主机计算机上工作。 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. 这可能是服务器配置问题(在这种情况下,我正在寻找解决方法),但是正在处理.net扩展名的事实很奇怪-因为这意味着中等信任级别的问题不应适用,但是问题关于服务器上扩展处理的信息可能要优先于.net应用程序。

The server is shared hosting (therefore I am unable to alter the machine.config files), is IIS6 using 4.0. 服务器是共享主机(因此我无法更改machine.config文件),是使用4.0的IIS6。

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). 您需要在IIS 6.0中配置网站,以将所有扩展名(包括无扩展名的路径,称为通配符扩展名映射 )路由到ASP.NET ISAPI dll(并禁用文件是否存在检查)。

You can of course do this mapping selectively only for those extensions that you want to route via ASP.NET code. 当然,您只能为要通过ASP.NET代码路由的那些扩展有选择地进行此映射。 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. 在没有此类映射的情况下,IIS不会将对未知扩展名的请求转发到ASP.NET(并且路由代码甚至都不会出现)-而是IIS会将扩展名传递给默认(静态文件)处理程序,如果文件存在,则该处理程序将发出404不存在。

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 请参阅描述这些步骤的文章(适用于ASP.NET MVC,但适用于您的情况): 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 在文章结尾处,作者给出了如何添加通配符脚本映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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