When rendering UserControl dynamically, How do I make it use the current session?
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?
Make sure your HttpHandler implements marker interface IRequiresSessionState.
精彩评论