Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 how to hook the OnSessionStart event?

I start MVC4 project in VS2010, In Global.asax.cs like :

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth(); 
    }
}

how to register Session Start event ?

like image 802
Robin Li Avatar asked Sep 23 '13 08:09

Robin Li


People also ask

How can use session variable in MVC?

Sessions values can be stored for the duration of the visitor's session on your site. Most cases, they are stored in server memory. You can configure session to store either in State server or in SQL Server. In ASP.NET MVC, you can create and access session variables using HttpContext.

Can we use session in MVC?

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same. But like session variable these elements cannot preserve values for all requests; value persistence varies depending the flow of request.


1 Answers

You can just add this to your Global.asax.cs like this:

protected void Session_Start(object sender, EventArgs e)
{
    // event is raised each time a new session is created     
}

protected void Session_End(object sender, EventArgs e)
{
    // event is raised when a session is abandoned or expires
}

You should also probably check http://msdn.microsoft.com/en-us/library/bb470252(v=vs.100).aspx

Cheers!

like image 96
MonkeyCoder Avatar answered Oct 26 '22 19:10

MonkeyCoder