开发者

When rendering UserControl dynamically, How do I make it use the current session?

开发者 https://www.devze.com 2023-03-23 12:12 出处:网络
I am 开发者_开发问答rendering a custom usercontrol from a HttpHandler like such: public void ProcessRequest(HttpContext context)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号