Accessing Session in the ServiceStack Razor View

I am trying to access the session inside a ServiceStack Razor View (Partial). In this case I am just trying to render our the menu which exists in the session.

@(new HtmlString(this.SessionAs<CustomUserSession>().MenuHtmlString))

I get this error:

Response Status

Error Code NullReferenceException Message Object reference not set to an instance of an object. Stack Trace [BOPBasicInfo1VM: 1/31/2014 9:46:45 PM]: [REQUEST: {QuoteNumber:1,AgencyId:0,Errors:[],IsValid:False}] System.NullReferenceException: Object reference not set to an instance of an object. at ASP._BOPBasicInfo1VM.Execute() at ServiceStack.Razor.ViewPage 1.WriteTo(StreamWriter writer) at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPageWithLayout(IRequest httpReq, IResponse httpRes, Object model, IRazorView page, Func 1 layout) at ServiceStack.Razor.Managers.RazorPageResolver.ResolveAndExecuteRazorPage(IRequest httpReq, IResponse httpRes, Object model, RazorPage razorPage) at ServiceStack.Razor.Managers.RazorPageResolver.ProcessRequest(IRequest httpReq, IResponse httpRes, Object dto) at ServiceStack.Formats.HtmlFormat.<>c_DisplayClass2.b__0(IViewEngine x) at System.Linq.Enumerable.Any[TSource](IEnumerable 1 source, Func 2 predicate) at ServiceStack.Formats.HtmlFormat.SerializeToStream(IRequest request, Object response, IResponse httpRes)

Am I trying to access the session correctly? Any ideas as to why this is failing?

As far as I know there is no break point/debug support in the SS Razor Views, has that changed with the latest version v4.0.8.0+?


The issue turned out to be with the

@(new HtmlString())

instead of the

this.SessionAs<CustomUserSession>().MenuHtmlString

The way that I figured that out was by creating a static method on a class and passing "this" into it so that I could debug.

    public static HtmlString GetMenu(ServiceStack.Razor.ViewPage page)
    {
        return new HtmlString(page.SessionAs<FMIC.DTOs.Entities.CustomUserSession>().MenuHtmlString);
    }

And calling it like this from the view.

@(ViewPageExtensions.GetMenu(this))

I will also add a comment from Demis (mythz) from the ServiceStack Forums

To help with debugging I would call your own method from Razor views so you can put a breakpoint and inspect the values passed in.

You should also hopefully be able to debug v4.09 source code using the source symbols published with NuGet, some instructions: https://github.com/ServiceStack/ServiceStack/wiki/Debugging#wiki-debugging-source-symbols-in-nuget-packages

链接地址: http://www.djcxy.com/p/65514.html

上一篇: ServiceStack服务设计

下一篇: 在ServiceStack Razor视图中访问会话