How to handle former global event handlers in ASP.NET 5? - asp.net

Actually, we use ASP.NET 4.5 (VS 2013) and want to replace Global.asax.cs with new Startup.cs file, which comes from OWIN specification.
We log the start and the end of application using Application_Start and Application_End handlers in this way:
protected void Application_Start(object sender, EventArgs e)
{
_log.Info("The app starts.");
}
protected void Application_End(object sender, EventArgs e)
{
_log.Info("The app ends.");
}
But, as I know there is no such handlers in the new Startup.cs file. So, my question is - how can we do that?

You already know when the application is starting since Startup.cs is executing then. For the Application_End event have a look at this answer: Is there Application_End from Global.asax in Owin?

Related

How to handle Application_AcquireRequestState, Session_Start and Session_End in ASP.NET 4.5 using OWIN?

We use ASP.NET 4.5 (VS 2013) and want to replace Global.asax.cs with new Startup.cs file, which comes from OWIN specification.
We need to replace Application_AcquireRequestState, Session_Start and Session_End handlers with something in Startup.cs file. It looks as following in Global.asax.cs:
protected void (Object sender, EventArgs e)
{
SessionCounter.AddSessionPage(Context);
}
protected void Session_Start(Object sender, EventArgs e)
{
}
protected void Session_End(Object sender, EventArgs e)
{
LoginLog.RegisterLogOff(Context);
SessionCounter.AbandonSession(Context);
}
How can we do that?
OWIN has no definition for session and cannot fully replace the Global.asax.cs file.
Try ASP.NET 5, it moves everything from Global.asax.cs to Startup.cs. https://github.com/aspnet/home

Is it possible to handle events in ASP.NET app that uses OWIN?

We have ASP.NET application with Global.asax.cs file where we use application event handlers as following:
protected void Application_Start(object sender, EventArgs e)
{
log.Info("The App started.");
}
protected void Application_End(object sender, EventArgs e)
{
log.Info("The App finished.");
}
We want to use OWIN now, which means adding Startup.cs file. Is there any way to move handlers from Global.asax.cs то Startup.cs file or something else where we can put our logs?
OwinContext in Startup.Configuration() is different from the traditional ASP.NET HttpContext which exists in MvcApplication.Application_Start(). Both are using different pipelines.
And because of that you can't use MvcApplication.Application_Start() in Startup.Configuration()

Initialize variables before having anyone enter the web app

How can I initialize variables before my Web Application starts? I want to have, for example an Application type, to count number of logged in users in my website (it's an example, I have more variables that need initialization). How can I perform that?
It could be very useful if there was some event that would be called at the start of the Web App. I thought that I could check in each Page_Load event if a certain Session called, e.g Session["Started"] is null, and if it, redirect to an .aspx page for initialization. Or, even better, have a class called, e.g MyPage which inherits from System.Web.UI.Page and have her constructor get a function as an argument, which will be called after the initialization of the base class Page_Load.
Maybe I'm just bothering, is there any built-in event called that I can overwrite to initialize everything I want in the beginning?
You can use the Global.asax file for this. In particular you can use the Application_Start, Session_Start, Session_End events.
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
}
Here for more informations.

What is wrong with my .net routes?

I have followed a few tutorials online and they all seem to show the same logic for .net routing using ASP.net web forms. When I execute the URL below I get a 404 error. Test.aspx is in the root folder of this application.
http://www.mydomain.com/member/abc
Here is my global.asax contents:
<%# Application Language="C#" %>
<%# Import Namespace="System.Web.Routing" %>
<script runat="server">
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"TestABC",
"member/{name}",
"~/Test.aspx");
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
Is there something I need to do with my web.config file?
Any help is greatly appreciated.
I'm guessing that your routing module is not triggered when you hit the iis server.
As a test to verify that this is the cause : change your webconfig to run all managed modules upon a request.
You need to set this :
<modules runAllManagedModulesForAllRequests="true">
If that solved it you can go read this resource on why to not do that :)

On-Session-expire-event?

I'm programming an MVC3 application. Now I hava to call a script if the users session expire.
Is there something like a event on-session-expire, that get fired when the user session expired?
Thanks a lot!
In your Global.asax
you can create a
protected void Session_End(object sender, EventArgs e) { }
method which should be called when a session ends.

Resources