Creating a System.Web.UI.Page programatically in IHTTPHandler

Posted by ObiWanKenobi on Stack Overflow See other posts from Stack Overflow or by ObiWanKenobi
Published on 2009-01-25T11:50:40Z Indexed on 2010/03/14 19:05 UTC
Read the original article Hit count: 331

Filed under:
|

I am trying to use the ASP.NET (3.5) "Routing Module" functionality to create custom pages based on the contents of the URL.

Various articles, such as this one: http://blogs.msdn.com/mikeormond/archive/2008/05/14/using-asp-net-routing-independent-of-mvc.aspx explain how to use ASP.NET Routing to branch to existing pages on the web server.

What I would like to do is create the page on-the-fly using code.

My first attempt looks like this:

public class SimpleRouteHandler : IRouteHandler
{

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string pageName = requestContext.RouteData.GetRequiredString("PageName");

        Page myPage = new Page();
        myPage.Response.Write("hello " + pageName);
        return myPage;

    }

}

But this throws an HTTPException saying "Response is not available in this context." at the Response.Write statement.

Any ideas on how to proceed?

UPDATE: In the end, I went with an approach based on IHttpModule, which turned out to be rather easy.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about routing