对带有 .ashx 文件的 Web 表单使用 URL 路由

Ben*_*man 4 .net c#

我正在开发一个使用 URL 路由的基于 .NET 3.5 Web 窗体的网站。到目前为止,我已经创建了几条路线,我没有遇到任何问题。我现在有一个 .ashx 文件,当有人单击链接时,该文件将处理将 .pdf 文件从 SQL Server 中的表发送到网站。通常,当我创建一个处理程序时,它看起来像这样:

return BuildManager.CreateInstanceFromVirtualPath("~/ViewItem.aspx", typeof(Page)) as Page;
Run Code Online (Sandbox Code Playgroud)

对于我的 .ashx 文件,我尝试过:

return BuildManager.CreateInstanceFromVirtualPath("~/FileServer.ashx", typeof(Page)) as Page;
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为 fileserver.ashx 不是页面,因此将其转换为 typeof(Page)) 因为 Page 将失败。我如何将 VirtualPath 转换为而不是 Page 或者我应该以其他方式执行此操作。

Not*_*tMe 5

使用typeof(IHttpHandler)代替typeof(Page)

也就是ashx文件的基类,也把返回类型从as Page改成as IHttpHandler。然后更新依赖于它是页面的任何其他代码。