繁体   English   中英

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

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

我在自定义扩展和拦截现有处理程序时遇到了很多问题。

我想做什么

基于持久性选项,我希望所有“虚拟”扩展都由集合处理程序处理。 所有页面都是动态构建的,站点上没有实际的文件。 该网站填充内容,形成html输出并将其作为Web结果返回。 这是必需的,因为我正在两台服务器之间建立胖/瘦关系。 瘦服务器将简单地将请求传递到胖服务器-胖服务器在该服务器上处理请求,并在线路下发回响应。 该项目适用于动态的多域内容管理系统。 瘦服务器可能不兼容.net(因此需要外部请求),但是将优化.net(因此需要处理程序)。

问题

我想要的是重新路由现有扩展-aspx; php; html。 我已经在我的本地环境中使用设置适当处理程序的自定义HttpModule实现了此目的。 我已经探索了在config中设置标签的方法,但是扩展使用持久化的动态规则进行了重新路由。

如前所述,该解决方案适用于localhost。 上传后,.Net扩展名将由模块正确处理,但是任何自定义扩展名或非.net扩展名均会返回404错误。

寻求替代方法,我已经尝试在Global内进行路由,但这也不起作用。

我还尝试过使用注册自定义扩展名...但是每个都遇到了相同的结果-找不到404。


全局路由尝试:

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

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

自定义模块(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;
        }
    }

所有处理程序都是标准的,实现以下内容:

public class SqlRequestHandler : IHttpHandler, IRequiresSessionState, IRouteHandler

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

服务器是共享主机(因此我无法更改machine.config文件),是使用4.0的IIS6。

感谢您对如何解决此问题的任何建议。 麦克风

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

当然,您只能为要通过ASP.NET代码路由的那些扩展有选择地进行此映射。 但是,如果您没有预定义的扩展集,通配符映射将更加有用。

在没有此类映射的情况下,IIS不会将对未知扩展名的请求转发到ASP.NET(并且路由代码甚至都不会出现)-而是IIS会将扩展名传递给默认(静态文件)处理程序,如果文件存在,则该处理程序将发出404不存在。

请参阅描述这些步骤的文章(适用于ASP.NET MVC,但适用于您的情况): http : //haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6- walkthrough.aspx
在文章结尾处,作者给出了如何添加通配符脚本映射

暂无
暂无

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

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