locked
Calling ASP.NET web handler from xhtml page RRS feed

  • Question

  • User-596730513 posted

    I am trying to call an ASP.NET web handler from an xhtml page to display information. It calls the web handler ok but the information is not displayed. I know it calls the handler as I can see this as I step through debug. This is easy if implemented calling JavaScript (using document.write) but I want to access a database in the ASP code.

    The code I am using is:

    ----------------

    ASP.NET 'Handler.ashx' default code:

    ----------------

    <%@ WebHandler Language="C#" %>

    using System;
    using System.Web;

    public class Handler : IHttpHandler {
       
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    }

    ----------------

    xhtml code:

    ----------------

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <script type="text/C#" src="Handler.ashx"></script>
    </body>
    </html>
    ----------------

     

    Thanks

    Saturday, August 8, 2009 2:32 PM

Answers

  • User1696475319 posted

    If this is your own website you want to display it on, then you can use a Literal control.  The Literal control accepts any string, even HTML ones, and displays it on the page as part of the rendered HTML.

    If this is a third party website and you want them to display some HTML from your own website, then you're going to have to get a little fancy. You can ask them to add an IFRAME (yes, I said it) to their page which points to a page on your site.  Or ask them to place a JavaScript reference to a .js file on your site which in turn performs a document.write and writes some HTML out - you'd need to make the javascript dynamic or you can make the javascript write an IFRAME instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 10, 2009 6:01 PM

All replies

  • User1696475319 posted

    Handlers don't work like that - you're expecting to be able to write to the ASPX before interpretation and rendering to the browser. To break your example down:


    <script type="text/C#" src="Handler.ashx"></script>

    Is meant for C# code, and when using inline ASP.NET in a web page.  So it should also have a runat="server". Further, when the page is run/executed, the parser will see the src="handler.ashx", but this will mean nothing.  The code needs to be within the script tags on that page, or in the codebehind of that page.  Note that the only thing you'll get when you browse to Handler.ashx is a bit of text saying "Hello World".  In and of itself, it is not code and can do nothing.

    What are you trying to accomplish here, and why aren't you using regular code?

    Sunday, August 9, 2009 7:22 AM
  • User-596730513 posted

    Thanks for your reply.

    What I want to accomplish is the ability to dynamically create and display html on regular xhtml web sites from my ASP.NET website. The ASP.NET website has a SqlServer database which will determine the html returned dependant on which xhtml website called it.

    So what I need to know is if an ASP.NET website can return html to be displayed on an xhtml website and how this is achieved. i.e. Just like an xhtml website calling a bit of JavaScript on any website where the JavaScript uses 'document.write('Blah blah blah')'.

    Thanks

     

    Sunday, August 9, 2009 12:54 PM
  • User-1136466523 posted

     Hi,

    So what I need to know is if an ASP.NET website can return html to be displayed on an xhtml website and how this is achieved

    I think what you want to know is how to configure XHTML Rendering in ASP.NET Web Sites, right?

    If so, you may try the following article:

    http://msdn.microsoft.com/en-us/library/ms178159.aspx

    Thanks.

    Sunday, August 9, 2009 11:02 PM
  • User-319574463 posted

    So what I need to know is if an ASP.NET website can return html to be displayed on an xhtml website and how this is achieved.

    For help on getting an ASP.NET to render XHTML compliant pages, please see XHTMLFIX at http://xhtmlfix.codeplex.com/

    Once rendering XHTML markup, you can scrape the page, do you need help for that?

    Monday, August 10, 2009 11:36 AM
  • User-596730513 posted

    Thanks for your replies but I still don't seem to have a solution (maybe I don't understand your answers). I will try and give a very simple example of what I want to achieve:

    Imagine an html website with a web page that has the following xhtml:

    -----------------------

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title>Test Title</title>

    </head>

    <body>

        <script type="text/javascript" src="HelloWorld.js"></script>

    </body>

    </html>

    -----------------------

    Then we have the javascript file called ‘HelloWorld.js’:

    -----------------------

    document.write('Hello World');

    -----------------------

     

    **** All this does is display ‘Hello World’ in a web browser with a title of ‘Test Title’. ****

    Now… I want to replace the javascript file with an ASP.NET file in a different website. Can this be done? Can an ASP.NET page produce inline html for another html website?

    Monday, August 10, 2009 4:57 PM
  • User1696475319 posted

    If this is your own website you want to display it on, then you can use a Literal control.  The Literal control accepts any string, even HTML ones, and displays it on the page as part of the rendered HTML.

    If this is a third party website and you want them to display some HTML from your own website, then you're going to have to get a little fancy. You can ask them to add an IFRAME (yes, I said it) to their page which points to a page on your site.  Or ask them to place a JavaScript reference to a .js file on your site which in turn performs a document.write and writes some HTML out - you'd need to make the javascript dynamic or you can make the javascript write an IFRAME instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 10, 2009 6:01 PM
  • User-596730513 posted

    Thanks Mendhak

    Your second paragraph is the answer. I know <iframe> is not supported in XHTML 1.1 but a mixture of <iframe>,  <object> and some JavaScript or ASPX to detrmine the browser should do it.

    Thanks

    Tuesday, August 11, 2009 4:34 PM
  • User1696475319 posted

    IFRAME isn't, but you can make an IFRAME using OBJECT and stay compliant.  This will validate.

    <object id="whatever" data="yourPageName.aspx">
    
    Your browser is pointless and should not exist.
    
    </object>


    Thursday, August 13, 2009 3:27 AM