在 C# MVC 项目中将会话添加到假 httpContext

发布于 2024-10-02 16:58:37 字数 1775 浏览 8 评论 0原文

如何将会话添加到 fakeContext ?

我们为部分请求构建此函数,其中内容必须作为字符串返回。 只是我们现在在部分请求中没有会话。

我无法像 fakeContext.Session = HttpContext.Current.Session 这样添加它们

有人建议吗?

    ///<summary>
    /// Invoke the partial request and return the result as a string.
    ///</summary>
    ///<param name="context">The controller context to use.</param>
    ///<returns>A string containing the result of the partial request.</returns>
    public String InvokeAsString(ControllerContext context)
    {
        var stringBuilder = new StringBuilder();
        //create memory writer used for httpresponse.
        var memoryWriter = new StringWriter(stringBuilder);
        //create a fake response
        var fakeResponse = new HttpResponse(memoryWriter);
        //create a fake context.
        var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
        var oldPrincipal = context.HttpContext.User;
        fakeContext.User = oldPrincipal;

        //create a fake controllercontext to use for the default invoke action.
        var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), context.RouteData, context.Controller);                        

        var oldContext = HttpContext.Current;
        HttpContext.Current = fakeContext;

        ManagedWebSessionContext.Bind(
            HttpContext.Current,
            SessionManager.SessionFactory.OpenSession(new Interceptor()));

        //perform the default invoke action.
        Invoke(fakeControllerContext);
        HttpContext.Current = oldContext;

        //Flush memory and return output 
        memoryWriter.Flush();
        var content = stringBuilder.ToString();

        return content;
    }

How can i add the session to a fakeContext ?

This function have we build for partial request where the content must be returned as a string.
Only we don't have sessions now in the partial request.

And i can't add them like fakeContext.Session = HttpContext.Current.Session

Someone a suggestion ?

    ///<summary>
    /// Invoke the partial request and return the result as a string.
    ///</summary>
    ///<param name="context">The controller context to use.</param>
    ///<returns>A string containing the result of the partial request.</returns>
    public String InvokeAsString(ControllerContext context)
    {
        var stringBuilder = new StringBuilder();
        //create memory writer used for httpresponse.
        var memoryWriter = new StringWriter(stringBuilder);
        //create a fake response
        var fakeResponse = new HttpResponse(memoryWriter);
        //create a fake context.
        var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
        var oldPrincipal = context.HttpContext.User;
        fakeContext.User = oldPrincipal;

        //create a fake controllercontext to use for the default invoke action.
        var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), context.RouteData, context.Controller);                        

        var oldContext = HttpContext.Current;
        HttpContext.Current = fakeContext;

        ManagedWebSessionContext.Bind(
            HttpContext.Current,
            SessionManager.SessionFactory.OpenSession(new Interceptor()));

        //perform the default invoke action.
        Invoke(fakeControllerContext);
        HttpContext.Current = oldContext;

        //Flush memory and return output 
        memoryWriter.Flush();
        var content = stringBuilder.ToString();

        return content;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆离笙 2024-10-09 16:58:37

找到解决方案

fakeContext.Items.Add("AspSession", HttpContext.Current.Session);

通过这种方式,即使 fakeContext.Session 是只读的,您也可以更改会话

Found the solution

fakeContext.Items.Add("AspSession", HttpContext.Current.Session);

On this way you can change the session even as fakeContext.Session is readOnly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文