7

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

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

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

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

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

public class FileDownload : IHttpHandler
{
}
4

2 回答 2

4

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

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

于 2010-06-30T13:16:37.630 回答
1

听起来像一个IIS问题。

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

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

您还需要根据任何 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 路由的好文章,请参见此处。

祝你好运!

于 2010-06-29T21:29:42.637 回答