Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render an asp.net WebForm page from Global.asax?

for one reason or another I'm messing around with a "minimalistic" ASP.Net just for fun. I've disabled a lot of things and am attempting to reimplement things. One thing I can't quite figure out is how to render an ASP.Net page(aspx).

This is my progress so far:

//global.asax
    protected virtual void Application_BeginRequest (Object sender, EventArgs e)
    {
        HtmlTextWriter writer=new HtmlTextWriter(Response.Output);
        if(Request.Url.AbsolutePath.Substring(0,Math.Min(Request.Url.AbsolutePath.Length,8))=="/static/"){
            return; //let it just serve the static files
        }else if(Request.Url.AbsolutePath=="/test1"){
            test1 o=new test1();
            o.ProcessRequest(Context);
            o.RenderControl(writer);
            writer.Flush();
            writer.Close();
            Response.Flush();
        //  Response.Write(writer.ToString());

        }else{
            Response.ContentType="text/plain";
            Response.Write("Hi world!");
        }
        CompleteRequest();
    }

the /static/ bit works as does the "hi world". I can't get the /test1 route to work though. It reaches that point but all that is displayed is a black page.

I have a test1.aspx page with this designer content:

<%@ Page Language="C#" Inherits="namespace.test1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>test1</title>
</head>
<body>
    <form id="form1"> <!--just testing if two forms works and such-->

    </form>
    <form id="form2">
    <input type="text" id="test1" />
    </form>
</body>
</html>

and it has almost no code behind(just an empty function which doesn't matter)

What am I doing wrong here?

like image 285
Earlz Avatar asked Dec 04 '10 04:12

Earlz


2 Answers

Global.asax is a red herring. ASP.NET is successfully rendering the page you requested:

test1 o=new test1();

test1 is the code-behind class for the test1.aspx page. That's not what you want though, see? Everything you're expecting to see comes from the test1.aspx file. What you need to do is tell ASP.NET to render test1.aspx to Response.Output:

using (var o = (Page) BuildManager.CreateInstanceFromVirtualPath("/test1.aspx", typeof (Page))) {
    o.ProcessRequest(Context);
}
like image 80
Michael Kropat Avatar answered Sep 23 '22 10:09

Michael Kropat


You can use HttpContext.Current.Server.Execute here. See HttpServerUtility.Execute.

like image 42
Cheng Chen Avatar answered Sep 22 '22 10:09

Cheng Chen