输入路径时,ASHX处理程序不会触发

时间:2017-01-18 17:12:51

标签: asp.net httphandler ashx virtual-directory

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

e.g。

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在本地运行

由于

1 个答案:

答案 0 :(得分:0)

我认为你很亲密。有一件事是您需要将文件写入响应。

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 替换为 授权 标记。

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

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

<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

Reqeust URL应该是这样的 -

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

OR

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

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

如果它仍然不起作用,请创建一个新的Web应用程序,并测试上面的代码。我已经测试过,它运行正常。