How to create a session variable in DotVVM view mode?

I create a site in DotVVM, and when I try the following line of code, but I get an error: NullReferenceException

HttpContext.Current.Session.Add ("Value", Item3);
+4
source share
2 answers

DotVVM is OWIN middleware , so you need to configure OWIN first to enable the session. First, you need to declare this method, which includes an ASP.NET session:

public static void RequireAspNetSession(IAppBuilder app) {
    app.Use((context, next) =>
    {
        var httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
        httpContext.SetSessionStateBehavior(SessionStateBehavior.Required);
        return next();
    });

    // To make sure the above `Use` is in the correct position:
    app.UseStageMarker(PipelineStage.MapHandler);
}

Then Startup.cscall it in the file :

app.RequireAspNetSession();

Then you can use HttpContext.Current.Session["key"]to access your session state.

+7
source

You can save the object in the session by doing:

Session["Value"] = Item3;

You can get the object from the session by doing:

object value = Session["Value"];

, , , Item3 - , :

string value = (string)Session["Value"];

, .

0

All Articles