如何从Global.asax呈现asp.net WebForm页面?

Ear*_*rlz 8 c# asp.net webforms global-asax

出于这样或那样的原因,我只是为了好玩而乱搞"简约"的ASP.Net.我已经禁用了很多东西,并试图重新实现.我无法弄清楚的一件事是如何呈现ASP.Net页面(aspx).

这是我目前的进展:

//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();
    }
Run Code Online (Sandbox Code Playgroud)

/ static/bit和"hi world"一样工作.我不能让/test1路线上班.它达到了这一点,但所有显示的都是黑页.

我有一个带有此设计器内容的test1.aspx页面:

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

它几乎没有代码(只是一个无关紧要的空函数)

我在这做错了什么?

Mic*_*pat 5

Global.asax是一个红鲱鱼.ASP.NET成功呈现您请求的页面:

test1 o=new test1();
Run Code Online (Sandbox Code Playgroud)

test1test1.aspx页面的代码隐藏类.那不是你想要的,看到了吗?您期望看到的所有内容都来自test1.aspx文件.您需要做的是告诉ASP.NET将test1.aspx呈现给Response.Output:

using (var o = (Page) BuildManager.CreateInstanceFromVirtualPath("/test1.aspx", typeof (Page))) {
    o.ProcessRequest(Context);
}
Run Code Online (Sandbox Code Playgroud)


Che*_*hen 4

你可以HttpContext.Current.Server.Execute在这里使用。请参阅HttpServerUtility.Execute