ASP.Net 4.0 - 如何从ASHX中访问RouteData?

7
我的网站有一个处理程序(FileDownload.ashx),用于处理所有文件下载请求。
我最近将网站迁移到了ASP.NET 4.0,并且现在广泛使用路由。当处理页面请求(aspx)时,一切正常,但是我的处理程序无法正常工作 - 我遇到了以下错误:
“类型“.Handlers.FileDownload”未继承自“System.Web.UI.Page”。”
这很有道理,因为路由仅在页面中实现。
要能够同时使用路由和我的.ashx,我需要采取哪些步骤?我想能够从路由中提取RouteData.Values。
public class FileDownload : IHttpHandler
{
}
2个回答

4

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有关。

请参阅这里,了解有关ASP.NET 4路由在IIS6和IIS7上的良好文章。

祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接