开发者

Custom HttpHandler Error: Could not load type 'FileProtectionHandler'

开发者 https://www.devze.com 2023-03-16 22:18 出处:网络
I am trying to implement a Custom HttpHandler (for the first time), I have been given a tutorial to follow but couldn\'t get it to work. I then found another tutorial but couldn\'t get that to work, t

I am trying to implement a Custom HttpHandler (for the first time), I have been given a tutorial to follow but couldn't get it to work. I then found another tutorial but couldn't get that to work, they are both giving me the same error message.

The custom handler is to protect people from downloading certain file types, although i think the error is somekind of configuration problem as I can't get the website to work at all once I add the httpHandlers to the Web.Config file.

    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>

If you require more code please let me know.

Thanks for any help. J.

    <%@ 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 Impleme开发者_运维百科nts IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

Custom HttpHandler Error: Could not load type 'FileProtectionHandler'


I had similar problem. Solution was in root namespace defined in properties. In my code I do not have namespace, so in this case you need to use

type="[namespace or root namespace].[your class name]"


Try filling out the namespace that the class lives in as well as the assembly it's built to.

Something like this

<add verb="*" path="*.pdf" type="FileProtectionHandler, Beswick"/>

or possibly this

<add verb="*" path="*.pdf" type="Beswick.FileProtectionHandler, Beswick"/>

or this

<add verb="*" path="*.pdf" type="Beswick.FileProtectionHandler"/>


Just had the same problem adding a new IHttpHandler to an existing project. The handler I added had build action property "Content" rather than "Compile". Changing it to compile fixed the issue


.NET 4.5 WebForm ,it was fixed for me after adding the ProjectName.ClassName

<httpHandlers>
<add verb="*" path="scripts/*" validate="false" type="ProjectName.NoAccessHandler"/>
</httpHandlers>

and I have extra part not positive if it actaully do anything under system.webServer -> Handlers I have this

<Handlers>
      <add verb="*"  path="scripts/*" name="NoAccessHandler"  
        preCondition="integratedMode" type="NoAccessHandler"
        resourceType="Unspecified"/>
</Handlers>


I've just come back to this issue after a lengthy break from it. I'm not sure if i've got it fully working as yet as from first testing it's not protecting the file if a user isn't logged into the website, but I am no longer getting the error message.

I found the fix to the problem here: HttpHandler 101 FAIL


If none of these answers works, and your project is a Web Appliccation (as opposed to a Web Page as in HttpHandler 101 FAIL), check the build output path. I had recently changed my platform to x86, which changed Properties ->Build -> Output path to

bin\x86\Debug

I changed this back to

bin\

and it worked.


I encountered a similar error while debugging an Azure Web App locally (the error persisted when deployed to Azure). I suspect that the error has something to do with locally stored configuration/compilation files, which are not updated properly even when the solution is cleaned and rebuilt. I had two different projects which produced identically named dll's (albeit to different locations), not sure if this had any effect on the issue.

After lengthy experimentation, the solution for me was to go to Solution Explorer in Visual Studio, right-click on the project --> Properties. Under the Application tab, change the Target framework to something else (I changed 4.6 to 4.6.1). You will get a prompt saying that the project will be reloaded, click OK. After reload, do the same thing reverting back to your original version (4.6 for me). This fixed the issue for me.

It would be nice to understand the root cause of the issue. I still get the error sometimes when reopening the project, and I have to go through the above steps again.


Changing the order and keeping as below in config file fixed my issue.

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号