输入路径时 ASHX 处理程序不会触发
ASHX handler wont fire when path is entered
我有一个可以直接从 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
谢谢
我觉得你很亲近。一件事是你需要写文件来响应。
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.[=16= 中添加 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,测试上面的代码。我已经测试过,它工作正常。
我有一个可以直接从 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
谢谢
我觉得你很亲近。一件事是你需要写文件来响应。
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.[=16= 中添加 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,测试上面的代码。我已经测试过,它工作正常。