【问题标题】:ASP.Net 4.0 - How to access RouteData from within an ASHX?ASP.Net 4.0 - 如何从 ASHX 中访问 RouteData?
【发布时间】:2011-03-09 17:19:08
【问题描述】:

我的网站有一个处理所有文件下载请求的处理程序 (FileDownload.ashx)。

我最近将我的站点迁移到 ASP.NET 4.0,它现在广泛使用路由。处理页面请求 (aspx) 时一切正常,但它不适用于我的处理程序 - 我遇到以下错误:

类型“.Handlers.FileDownload”不继承自“System.Web.UI.Page”。

这是有道理的,因为路由只在页面中实现。

我需要采取哪些步骤才能同时使用路由和我的 .ashx?我希望能够从路由中提取RouteData.Values

public class FileDownload : IHttpHandler
{
}

【问题讨论】:

    标签: asp.net routes httphandler ashx


    【解决方案1】:

    我最终需要手工制作一个处理程序,但这很容易:http://haacked.com/archive/2009/11/04/routehandler-for-http-handlers.aspx

    .Net 4.0 本身不支持 IHttpHandlers 的路由处理。

    【讨论】:

      【解决方案2】:

      听起来像是 IIS 问题。

      如果您尝试使用 ASP.NET 开发服务器 (Cassini),这是否有效?

      如果您使用的是 IIS6,则需要使用通配符应用程序映射 - 请参阅 here

      您还需要根据任何 ASPX 页面创建路由,如下所示:

      public static void RegisterRoutes(RouteCollection routes)
      {
          string[] allowedMethods = { "GET", "POST" };
          HttpMethodConstraint methodConstraints = new HttpMethodConstraint(allowedMethods);
      
          Route fileDownloadRoute = new Route("{foo}/{bar}", new FileDownload());
          fileDownloadRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } };
      
          routes.Add(fileDownloadRoute);
      }
      

      你做到了吗?如果是这样,我会说你的问题肯定是 IIS。

      有关 IIS6 和 IIS7 的 ASP.NET 4 路由的好文章,请参阅 here

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2015-12-08
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        • 2019-08-13
        • 1970-01-01
        • 2010-10-25
        相关资源
        最近更新 更多