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

发布于 2024-09-07 16:47:47 字数 401 浏览 5 评论 0原文

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

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

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

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

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

public class FileDownload : IHttpHandler
{
}

My web site has a handler (FileDownload.ashx) that deals with all file download requests.

I've recently migrated my site to ASP.NET 4.0, and it now uses routing extensively. Everything works fine when dealing with page requests (aspx), but it's not working with my handler - I encounter the following error:

Type '.Handlers.FileDownload' does not inherit from 'System.Web.UI.Page'.

This makes sense, as routing is only implemented in the page.

What steps do I need to take to be able to use routing and my .ashx together? I want to be able to extract RouteData.Values from the route.

public class FileDownload : IHttpHandler
{
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2024-09-14 16:47:56

听起来像是 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 路由。

祝你好运!

Sounds like an IIS problem.

Does this work if you try and use the ASP.NET Development Server (Cassini) ?

If you're using IIS6 you'll need to use Wildcard Application Mappings - see here.

You'll also still need to create your routes as per any ASPX page, like this:

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);
}

Have you done that? If so, i'd say your problem is definetely with IIS.

See here for a good article on ASP.NET 4 Routing for both IIS6 and IIS7.

Good luck!

一梦浮鱼 2024-09-14 16:47:55

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

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

I needed to hand craft a handler in the end, but it was easy enough: http://haacked.com/archive/2009/11/04/routehandler-for-http-handlers.aspx

.Net 4.0 does not natively support route handling for IHttpHandlers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文