locked
HttpHandler issue: how to resume normal processing if condition in ProcessRequest fails RRS feed

  • Question

  • User1330301911 posted

    Hello,
    I have a question about httphandlers. Following is the definition of ProcessRequest in code:

     Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
            If context.Request.QueryString("fmt") = "source" Then

                Dim myFileStream As System.IO.FileStream = System.IO.File.OpenRead(context.Server.MapPath("~/Default.aspx.vb"))
                Dim myStreamReader As New System.IO.StreamReader(myFileStream)
                Dim myLiteral As New TextBox()
                myLiteral.TextMode = TextBoxMode.MultiLine
                myLiteral.Rows = 50
                myLiteral.Columns = 300

                myLiteral.Text = (myStreamReader.ReadToEnd())

                Dim myStringWriter As New System.IO.StringWriter()
                Dim myHtmlTextWriter As New HtmlTextWriter(myStringWriter)
                myLiteral.RenderControl(myHtmlTextWriter)

                context.Response.Clear()

                context.Response.Write(myStringWriter.ToString())

        

            End If
        End Sub

    The above generates source code for a page in a textbox if querystring fmt with value source is passed. However, if no querystring is passed, the normal ASP.NEt page should show up. Right now, if no querystring is passed, the page shows up as blank. I understand what the problem is: when no querystring is passed, because of the if check in ProcessRequest, nothing is done and so the page is empty.

    How do I resolve this so that if querystring is passed, source is show; otherwise normal processing of ASP.NET page should occur.

    Thanks all in advance

     

    Tuesday, January 13, 2009 10:32 PM

Answers

  • User437720957 posted

     An HttpHandler is an endpoint, and as such it's responsible for the entire rendering. It's all or nothing. You can still use use an HttpHandler, but the I suggest that you map it to a specific location or file extension instead.

    http://yourdomain/default.aspx?fmt=source

    could then be

    http://yourdomain/default.source

     

    If you want to use the querystring, you should write an HttpModule instead, which can intercept and modify the response if needed.

     

    Also...

    • if all you want is to create a textbox with some text in it, just create the necessary HTML yourself. Creating a TextBox control and rendering it is really overkill.
    • make sure you close the filestream when it has been used.   

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2009 3:19 PM

All replies

  • User-738352979 posted

     in web.config try this one

    <httpHandlers>
                <add verb="*" path="default.aspx?fmt=" type="CaptchaHandler"/>

    </ httpHandlers>

     

    or in global.asax

        void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
           

      If context.Request.QueryString("fmt") = "source" Then
          Context.Handler=ur custom handler

    else

       Context.Handler=defaulthanler

     

    Wednesday, January 14, 2009 3:55 AM
  • User1330301911 posted

    Thanks for your reply Som Nath. I tried to work with your suggestions but did not understand them completely. Therefore, your solutions did not work for me. Can you elaborate on the PreRequestHandlerExecute and how to specify handler names in context.handler? context.handler takes an object of httphandler. My httphandler is called Class1. How do i define this in context.handler?

    Nonetheless, I figured out a workaround for this. And here is my solutions. Basically, i checked for the querystring in a conditional if. If the IF condition fails, the else condition reads context.response.writefile(filename)

    Here is the complete code if someone is interested:

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

    If context.Request.QueryString("sourceview") = "true" Then

    Dim myFileNameToLoad As String = String.Empty

    If context.Request.QueryString("pagename") <> "" Then

    Dim myPageString As String = context.Server.HtmlEncode(context.Request.QueryString("pagename").Trim())

    myFileNameToLoad = context.Server.MapPath(myPageString)

    Else

    myFileNameToLoad = context.Server.MapPath(Path.GetFileName(context.Request.Path))

    End If

    Dim myFileStream As FileStream = File.OpenRead(myFileNameToLoad)

    Dim myStreamReader As New StreamReader(myFileStream)

    Dim srcCodeTextbox As New TextBox() With { _

    .TextMode = TextBoxMode.MultiLine, _

    .Rows = 30, _

    .Columns = 100}

    srcCodeTextbox.Text = (myStreamReader.ReadToEnd())

    Dim myStringWriter As New StringWriter()Dim myHtmlTextWriter As New HtmlTextWriter(myStringWriter)

    srcCodeTextbox.RenderControl(myHtmlTextWriter)

    context.Response.Clear()

    context.Response.Write(myStringWriter.ToString())

    Else

    context.Response.WriteFile(context.Request.Path)

     

    End If

     

    End Sub

    Wednesday, January 14, 2009 3:09 PM
  • User437720957 posted

     An HttpHandler is an endpoint, and as such it's responsible for the entire rendering. It's all or nothing. You can still use use an HttpHandler, but the I suggest that you map it to a specific location or file extension instead.

    http://yourdomain/default.aspx?fmt=source

    could then be

    http://yourdomain/default.source

     

    If you want to use the querystring, you should write an HttpModule instead, which can intercept and modify the response if needed.

     

    Also...

    • if all you want is to create a textbox with some text in it, just create the necessary HTML yourself. Creating a TextBox control and rendering it is really overkill.
    • make sure you close the filestream when it has been used.   

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2009 3:19 PM
  • User1330301911 posted

    Thanks for the clarification gunteman.

    Can you post some examples where httphandler can be used and where httpmodules should be used?

    Thursday, January 15, 2009 11:30 AM
  • User1330301911 posted

    Hey gunteman,

    I have created a test module as you suggested to process querystrings. Here is my implementation:

    Public Sub MyBeginRequest(ByVal sender As Object, ByVal e As EventArgs)

    Dim context As HttpApplication = CType(sender, HttpApplication)

    If Not String.IsNullOrEmpty(context.Request.QueryString("fmt")) Then

    context.Response.Clear()

    'context.Response.ContentType = "text/plain"

    Dim myFileStream As IO.FileStream = IO.File.OpenRead(context.Server.MapPath(IO.Path.GetFileName(context.Request.Path)))

    Dim myStreamReader As New IO.StreamReader(myFileStream)

    context.Response.Write("<pre>")While Not myStreamReader.EndOfStream

    context.Response.Write(context.Server.HtmlEncode(myStreamReader.ReadLine()))

    context.Response.Write("<br/>")

    End While

    context.Response.Write("</pre>")

    context.Response.End()

    End If

    End Sub

     

    The output is rendering as I wanted. However, can you suggest if this is a good approach? Or, can you please let me know any alternate way of accomplishing this task that would be more efficient?

    Another question is, when i set the contenttype property to text/plain, even then the browser renders the html. That's why i used server.htmlencode and enclosed the whole return string in pre. Is there a way around this (that is, instead of using pre tags, is there any other way)?

    Thanks again.

    Thursday, January 15, 2009 11:43 AM