Embed ashx text content into page

Last post 12-02-2007 7:35 PM by fhirzall. 4 replies.

Sort Posts:

  • Embed ashx text content into page

    11-30-2007, 11:28 PM
    • Member
      130 point Member
    • fhirzall
    • Member since 05-03-2006, 1:06 AM
    • Posts 44

    Hi everyone,

     How can I embed regular html/plain text content returned from my ashx handler into a page?

     I know that when I use ASHX handlers to get images, I just throw in an <img> tag with the src set to the handler, what do I do to embed plain text content?

     Thank you,

    -Feras
     

    Filed under:
  • Re: Embed ashx text content into page

    12-01-2007, 12:31 AM
    Answer

    Use Response.Write to belch out html/plain text.

    C#

    using System.Web;
    public class HelloWorldHandler : IHttpHandler
    {
        public HelloWorldHandler()
        {
        }
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest Request = context.Request;
            HttpResponse Response = context.Response;
            // This handler is called whenever a file ending 
            // in .sample is requested. A file with that extension
            // does not need to exist.
            Response.Write("&lt;html>");
            Response.Write("&lt;body>");
            Response.Write("&lt;h1>Hello from a synchronous custom HTTP handler.</h1>");
            Response.Write("&lt;/body>");
            Response.Write("&lt;/html>");
        }
        public bool IsReusable
        {
            // To enable pooling, return true here.
            // This keeps the handler in memory.
            get { return false; }
        }
    }

    VB

    Imports System.Web
    
    Public Class HelloWorldHandler
        Implements IHttpHandler
    
        Public Sub ProcessRequest(ByVal context As _
                System.Web.HttpContext) Implements _
                System.Web.IHttpHandler.ProcessRequest
            Dim request As HttpRequest = context.Request
            Dim response As HttpResponse = context.Response
            ' This handler is called whenever a file ending 
            ' in .sample is requested. A file with that extension
            ' does not need to exist.
            response.Write("<html>")
            response.Write("<body>")
            response.Write("<h1>Hello from a synchronous custom HTTP handler.</h1>")
            response.Write("</body>")
            response.Write("</html>")
        End Sub
    
        Public ReadOnly Property IsReusable() As Boolean _
                Implements System.Web.IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    End Class
    More resources on this MSDN page.  
  • Re: Embed ashx text content into page

    12-01-2007, 12:45 PM
    • Member
      130 point Member
    • fhirzall
    • Member since 05-03-2006, 1:06 AM
    • Posts 44

    Hi DarkNight,

    How would you then embed that handler into another web page? I know that if you just request that in the browser by typing in its URL it would work, but I'm trying to do something similiar to the way I did it when returning images (reference with an image tag from requesting page)

     Thanks for your help!

  • Re: Embed ashx text content into page

    12-01-2007, 1:09 PM

    Well in that case you could use the HttpWebRequest & HttpWebResponse classes to make a call to your handler and display it using StreamReader. This article can be of some help.

  • Re: Embed ashx text content into page

    12-02-2007, 7:35 PM
    • Member
      130 point Member
    • fhirzall
    • Member since 05-03-2006, 1:06 AM
    • Posts 44

    Thanks for your help Dark Knight...I decided to use a <script> tag that referenced my ASHX handler, and in my handler I had it return 'document.write(mystring)'. Looks like its working fine, thanks!

Page 1 of 1 (5 items)