【问题标题】:ASHX handler wont fire when path is entered输入路径时,ASHX 处理程序不会触发
【发布时间】:2017-01-18 17:12:51
【问题描述】:

我有一个可以直接从 URL 访问的文件目录

例如

http://website.com/DirectoryName/folder/downloadpdf.pdf

我需要控制谁可以访问这些文件,所以我创建了一个 ASHX 处理程序,以便检查用户的权限。

但是,当我访问 http://website.com/DirectoryName/ 时,处理程序不会被调用。

我相信这与我的配置有关,我有

  <location path="DirectoryName">
    <system.webServer>
      <handlers>
        <add name="PSHandler" path="DirectoryName/*" verb="*" type="ProjectName.PSHandler"/>
      </handlers>
    </system.webServer>
  </location>

不同的方式 - IIS 6?

  <location path="DirectoryName">
    <system.web>
      <httpHandlers>
        <add path="DirectoryName/*" verb="*" type="ProjectName.PSHandler, PSHandler"/>
      </httpHandlers>
    </system.web>
  </location>

上面有什么问题吗?

ashx 文件

<%@ WebHandler Language="VB" Class="PSHandler" %>

Imports System
Imports System.Web

Public Class PSHandler : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        context.Response.Write("Hello World")
    End Sub


    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

仅供参考,这是通过使用 IIS Express 在本地运行

谢谢

【问题讨论】:

  • 您的解决方案中有ashx 处理程序吗?或者你有一个实现ihttphandler 的通用类吗? ashx 通用处理程序需要在其自己的实际 url 上请求......或者这至少是 ashx 处理程序的正常用例。无论如何,您的配置中是否有额外的DirectoryName 级别?
  • 是的,我的应用程序根目录中有PSHandler.ashxfile。是的,它确实实现了ihttphandler DirectoryName 也在根目录中,所以它在 .com/ 之后

标签: asp.net httphandler ashx virtual-directory


【解决方案1】:

我认为你很接近。一件事是您需要将文件写入响应。

public class PSHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string fileName = Path.GetFileName(context.Request.Path);
        string filePath = context.Server.MapPath("~/DirectoryName/" + fileName);

        // Check permission of user

        context.Response.ContentType = "application/pdf";
        context.Response.WriteFile(filePath);
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

<system.webServer>
  <handlers>
    <add name="PS" path="DirectoryName/*" verb="*" 
       type="ProjectName.PSHandler, ProjectName" 
       preCondition="integratedMode"/>
  </handlers>
</system.webServer>

更新

首先,将 httpHandlers 替换为 authorization 标签。

<location path="DirectoryName">
   <system.web>
      <authorization>
         <deny users="*"/>
      </authorization>
   </system.web>
</location>

然后在应用程序的 web.config 中添加 handler

<system.webServer>
   <handlers>
      <add name="PS" path="DirectoryName/*" verb="*" 
           type="ProjectName.PSHandler, ProjectName" 
           preCondition="integratedMode"/>
   </handlers>
</system.webServer>

然后复制粘贴以下 PSHandler 代码。

Public Class PSHandler
    Implements IHttpHandler
    Public Sub ProcessRequest(context As HttpContext)
        Dim fileName As String = Path.GetFileName(context.Request.Path)
        Dim filePath As String = context.Server.MapPath(Convert.ToString("~/DirectoryName/") & fileName)

        ' Check permission of user. If permission denied, display 404 -
        ' context.Server.Transfer("~/404.aspx")

        context.Response.ContentType = "application/pdf"
        context.Response.WriteFile(filePath)
    End Sub

    Public ReadOnly Property IsReusable() As Boolean
        Get
            Return False
        End Get
    End Property
End Class

请求的 URL 应该是这样的 -

http://www.yourwebsite.com/DirectoryName/downloadpdf.pdf

http://localhost:xxxx/DirectoryName/downloadpdf.pdf

请确保 downloadpdf.pdf 文件存在于 DirectoryName 文件夹中。

如果还是不行,请新建一个Web Application,测试上面的代码。我已经测试过了,效果很好。

【讨论】:

  • 谢谢,你是说我不再需要&lt;Location&gt; 元素了吗?
  • 你是对的,你不需要它,但你仍然需要限制每个人访问该文件夹。 例如,&lt;location path="DirectoryName"&gt;&lt;system.web&gt;&lt;authorization&gt;&lt;deny users="*"/&gt;
  • 感谢您的帮助,但仍然不会触发 ProcessRequest 函数(不会击中我的断点)不知道我还能告诉您什么您不知道。 (实际上我已经发布了 ashx 文件)不过我会支持它
  • 您发布的 PSHandler 不起作用。我更新了我的答案。
  • 感谢您的详细回答,我将照您说的做,并创建一个新的网站应用程序,因为它无法正常工作。您说您测试了上述内容,但是我在您的处理程序代码中注意到这两种方法没有实现 IHttpRequest 例如实现 IHttpHandler.ProcessRequest。您是如何构建它的?
猜你喜欢
  • 2011-03-07
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多