Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom HttpHandler Error: Could not load type 'FileProtectionHandler'

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 Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

Property Pages Img

like image 645
Jammer Avatar asked Nov 30 '22 04:11

Jammer


2 Answers

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"/>
like image 21
Brian Dishaw Avatar answered Dec 06 '22 03:12

Brian Dishaw


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]"
like image 85
Anna Avatar answered Dec 06 '22 05:12

Anna