简体   繁体   English

无需请求即可呈现ASPX页面

[英]Rendering ASPX Page without a request

I have an ASPX page that I intend to use as a template to generate some HTML. 我有一个ASPX页面,我打算用它作为模板来生成一些HTML。 I have defined my markup and data bound controls and built a function to do all the data binding, call this.Render and return the HTML. 我已经定义了我的标记和数据绑定控件,并构建了一个函数来执行所有数据绑定,调用this.Render并返回HTML。 The function works just fine when called from Page_Load. 从Page_Load调用时,该函数工作正常。

My intent was to bypass page request and directly call the method and get the page HTML, but when I call the function without making a HTTP Request, none of my server side controls are initialized. 我的意图是绕过页面请求并直接调用该方法并获取页面HTML,但是当我在没有发出HTTP请求的情况下调用该函数时,我的服务器端控件都没有被初始化。

Is there any way I can call a method on a page, pass some params and get the HTML output without making a HTTP Request. 有没有什么办法可以在页面上调用方法,传递一些参数并获取HTML输出而无需发出HTTP请求。 I believe Server.Execute could do it but i cant find a way to pass params in it. 我相信Server.Execute可以做到但我无法找到一种方法来传递params。

I am calling the function like this 我正在调用这个函数

MyPage ThreadHTMLGenerator = new MyPage;
string threadHTML= ThreadHTMLGenerator.GenerateExpandedHTML(param1, param2, param3);

You need to use Server.Execute : 您需要使用Server.Execute

var page = new MyPage();
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);

Alternatively if you need to pass your own querystring parameters, you could do a WebRequest to yourself: 或者,如果您需要传递自己的查询字符串参数,则可以对自己执行WebRequest:

var request = WebRequest.Create("http://www.mysite.com/page.aspx?param1=1&param2=2");
var response = (HttpWebResponse)request.GetResponse ();
var dataStream = response.GetResponseStream ();
var reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM