开发者

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.RenderC开发者_如何学JAVAontrol(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?


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);
}


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


My first thought would be that you do not call the hidden Page.FrameworkInitialize. I'm not sure that it actually does anything for you in this scenario.

I also believe that Page.ProcessRequest will render directly to the provided HttpContext. See ProcessRequestMain in Reflector, during Render it calls this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output)).

We dont get to see where you get your Request and Response object from. Have you checked the HttpApplication sent to you as the sender parameter, so you're sure that you're using the correct objects?


This article shows how to render a UserControl from a web service. May be that can be of help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜