动态渲染 UserControl 时,如何使其使用当前会话?
我正在从 HttpHandler 渲染自定义用户控件,如下所示:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string workName = context.Request.QueryString["name"];
string workForm = RenderView("~/work/" + workName + ".ascx");
context.Response.Write(workForm);
}
public static string RenderView(string path)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
pageHolder.Controls.Add(viewControl);
StringWriter result = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, result, false);
return result.ToString();
}
问题是渲染的页面生成一个新会话。 (我可以通过将渲染的 HTML 的会话 ID 与当前会话 ID 进行比较来判断)
如何使动态页面使用当前会话?
注意:代码不在登录后面但将来会的。我应该记住什么问题,例如提供会话和身份验证 cookie 等吗?
I am rendering a custom usercontrol from a HttpHandler like such:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string workName = context.Request.QueryString["name"];
string workForm = RenderView("~/work/" + workName + ".ascx");
context.Response.Write(workForm);
}
public static string RenderView(string path)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
pageHolder.Controls.Add(viewControl);
StringWriter result = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, result, false);
return result.ToString();
}
The problem is that the rendered page generates a new session. (I can tell by comparing the session ID for the rendered HTML with the current session ID)
How do I make the dynamic page use the current session?
Note:The code is not behind a login but will be in the future. Are there any problems I should keep in mind like supplying the session and auth cookies etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您的 HttpHandler 实现标记接口 IRequiresSessionState。
Make sure your HttpHandler implements marker interface IRequiresSessionState.