渲染aspx页面没有请求 [英] Rendering ASPX Page without a request

查看:145
本文介绍了渲染aspx页面没有请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我打算为模板,用于生成一些HTML的ASPX页面。我定义我的标记和数据绑定控件和内置的函数来完成所有的数据绑定,调用this.Render并返回HTML。从Page_Load中调用时该功能工作得很好。

我的意图是绕过页面请求,并直接调用该方法,并得到了HTML页面,但是当我调用该函数而不进行HTTP请求,没有我的服务器端控件初始化。

有什么办法,我可以调用一个页面上的方法,通过一些PARAMS并获得HTML输出而不进行HTTP请求。我相信使用Server.Execute能做到这一点,但我不能找到一种方法来传递PARAMS在里面。

我打电话这样的功能

 我的页面ThreadHTMLGenerator =新我的页面;
字符串threadHTML = ThreadHTMLGenerator.GenerateExpandedHTML(参数1,参数2,参数3);


解决方案

您需要使用的使用Server.Execute

  VAR页=新我的页面();
StringWriter的作家=新的StringWriter();
HttpContext.Current.Server.Execute(页,作家,FALSE);

另外,如果你需要传递自己的查询字符串参数,你可以做一个WebRequest的对自己说:

  VAR请求= WebRequest.Create(http://www.mysite.com/page.aspx?param1=1&param2=2);
变种响应=(HttpWebResponse)request.GetResponse();
变种数据流= response.GetResponseStream();
VAR读者=新的StreamReader(数据流);
字符串responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

I have an ASPX page that I intend to use as a template to generate some 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. The function works just fine when called from 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.

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. I believe Server.Execute could do it but i cant find a way to pass params in it.

I am calling the function like this

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

解决方案

You need to use 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:

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

这篇关于渲染aspx页面没有请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆