How to display asp.net webporm page from global.asax?

for one reason or another, I fiddled with the "minimalist" ASP.Net just for fun. I have disabled many things and am trying to redo things. One thing that I cannot understand is how to make an ASP.Net (aspx) page.

This is my progress:

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

/ static / bit works just like hi world. However, I cannot use the /test1 route. It reaches this point, but everything 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 (just an empty function that doesn't matter)

What am I doing wrong here?

+8
c # webforms global-asax
source share
4 answers

Global.asax - red herring. ASP.NET successfully executes the requested page:

 test1 o=new test1(); 

test1 is the code class for the page test1.aspx. This is not what you want, see? Everything you expect to see comes from the file test1.aspx. What you need to do is show ASP.NET for rendering test1.aspx for Response.Output:

 using (var o = (Page) BuildManager.CreateInstanceFromVirtualPath("/test1.aspx", typeof (Page))) { o.ProcessRequest(Context); } 
+5
source share

Here you can use HttpContext.Current.Server.Execute . See HttpServerUtility.Execute .

+3
source share

My first thought was that you are not calling the hidden Page.FrameworkInitialize . I'm not sure if he really does anything for you in this scenario.

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

We are not going to see where you get the Request and Response object from. Have you checked the HttpApplication sent to you as the sender parameter, so you are sure you are using the correct objects?

+1
source share

This article shows how to display a UserControl from a web service . Maybe this can help.

+1
source share

Source: https://habr.com/ru/post/649824/


All Articles