5

セッションを 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;
    }
4

1 に答える 1

5

解決策を見つけました

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

このようにして、fakeContext.SessionがreadOnlyである場合でも、セッションを変更できます。

于 2010-12-02T09:51:58.250 に答える