我正在尝试实现自定义HttpHandler(第一次),我已经获得了一个可以遵循的教程但无法使其工作。然后我找到了另一个教程,但无法使它工作,他们都给了我相同的错误信息。
自定义处理程序是为了保护人们不要下载某些文件类型,虽然我认为错误是一些配置问题,因为一旦我将httpHandlers添加到Web.Config文件,我就无法使网站工作。
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Could not load type 'FileProtectionHandler'.
Source Error:
Line 47: </compilation>
Line 48: <httpHandlers>
Line 49: <add verb="*" path="*.pdf" type="FileProtectionHandler"/>
Line 50: </httpHandlers>
如果您需要更多代码,请告知我们。
感谢您的帮助。学家
<%@ WebHandler Language="VB" Class="FileProtectionHandler" %>
Imports System
Imports System.Web
Imports System.Web.Security
Imports System.IO
Imports System.Web.SessionState
Public Class FileProtectionHandler : Implements IHttpHandler
Private Function SendContentTypeAndFile(ByVal context As HttpContext, ByVal strFile As [String]) As HttpContext
context.Response.ContentType = GetContentType(strFile)
context.Response.TransmitFile(strFile)
context.Response.[End]()
Return context
End Function
Private Function GetContentType(ByVal filename As String) As String
' used to set the encoding for the reponse stream
Dim res As String = Nothing
Dim fileinfo As New FileInfo(filename)
If fileinfo.Exists Then
Select Case fileinfo.Extension.Remove(0, 1).ToLower()
Case "pdf"
If True Then
res = "application/pdf"
Exit Select
End If
End Select
Return res
End If
Return Nothing
End Function
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
答案 0 :(得分:14)
我有类似的问题。解决方案在属性中定义的根命名空间中。 在我的代码中,我没有命名空间,所以在这种情况下你需要使用
type="[namespace or root namespace].[your class name]"
答案 1 :(得分:3)
尝试填写该类所在的命名空间以及它所构建的程序集。
像这样的东西
<add verb="*" path="*.pdf" type="FileProtectionHandler, Beswick"/>
或可能
<add verb="*" path="*.pdf" type="Beswick.FileProtectionHandler, Beswick"/>
或者
<add verb="*" path="*.pdf" type="Beswick.FileProtectionHandler"/>
答案 2 :(得分:3)
在向现有项目添加新IHttpHandler
时遇到同样的问题。我添加的处理程序具有构建操作属性“Content”而不是“Compile”。将其更改为编译修复了问题
答案 3 :(得分:0)
经过长时间的休息后,我才回到这个问题。我不确定我是否已经完全正常工作,因为从第一次测试它不保护文件,如果用户没有登录到网站,但我不再收到错误消息。
我在这里找到了问题的解决方法:HttpHandler 101 FAIL
答案 4 :(得分:0)
如果这些答案都不起作用,并且您的项目是Web应用程序(而不是HttpHandler 101 FAIL中的Web页面),请检查构建输出路径。我最近将我的平台改为x86,它改变了属性 - &gt; Build - &gt;输出路径
bin\x86\Debug
我把它改回了
bin\
并且有效。
答案 5 :(得分:0)
我在本地调试Azure Web App时遇到类似错误(部署到Azure时仍然存在错误)。我怀疑该错误与本地存储的配置/编译文件有关,即使在清理和重建解决方案时也无法正确更新。我有两个不同的项目,它们产生了相同名称的dll(尽管位于不同的位置),不确定这是否会对这个问题产生任何影响。
经过漫长的实验,我的解决方案是在Visual Studio中转到解决方案资源管理器,右键单击项目 - &gt;属性。在Application选项卡下,将Target框架更改为其他内容(我将4.6更改为4.6.1)。您将收到一条提示,说明将重新加载项目,单击“确定”。重新加载后,做同样的事情恢复到原始版本(4.6对我来说)。这为我解决了这个问题。
理解问题的根本原因会很好。我有时在重新打开项目时会收到错误,我必须再次执行上述步骤。
答案 6 :(得分:0)
更改顺序并在配置文件中保留以下内容解决了我的问题。
<system.webServer>
<handlers>
<remove name="traceverbhandler" />
<remove name="optionsverbhandler" />
<add name="extensionlessurlhandler-integrated-4.0" path="*." verb="*" type="system.web.handlers.transferrequesthandler" />
<remove name="extensionlessurlhandler-integrated-4.0" />
</handlers>
</system.webServer>
答案 7 :(得分:0)
.NET 4.5 WebForm,添加了ProjectName.ClassName后,它已为我修复
<httpHandlers>
<add verb="*" path="scripts/*" validate="false" type="ProjectName.NoAccessHandler"/>
</httpHandlers>
如果我确实在system.webServer->处理程序下执行了任何操作,则我还有其他方面不能肯定
<Handlers>
<add verb="*" path="scripts/*" name="NoAccessHandler"
preCondition="integratedMode" type="NoAccessHandler"
resourceType="Unspecified"/>
</Handlers>