Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you wire up Application_BeginRequest() in asp.net-mvc

I see in global.asax.cs from an ASP.NET MVC project

 protected void Application_BeginRequest()  {  } 

but when I try to add this to my project, I don't see what is calling this method. I see that the base System.Web.HttpApplication has this event but I don't see anything overriding it or subscribing to this event.

Can someone explain how you wire up Application_BeginRequest in ASP.NET MVC?

like image 603
leora Avatar asked Jul 06 '11 01:07

leora


People also ask

What is Application_BeginRequest?

Application_BeginRequest is an event handler. It is part of the ASP.NET website system. The Application_BeginRequest method is executed on all requests handled by the ASP.NET runtime. Performance. This method is executed many times.

How do I redirect to a controller action in MVC from global ASAX?

We can redirect to controller actions from Global. asax in ASP.Net MVC 5 using the method - RedirectToControllers. The RedirectToControllers() method provided by MVC 2 + versions. Example, it might help you to understand more about the same.

What is mvc6?

MVC 6 is a part of ASP.NET 5 that has been designed for cloud-optimized applications. The runtime automatically picks the correct version of the library when our MVC application is deployed to the cloud. The Core CLR is also supposed to be tuned with a high resource-efficient optimization.


2 Answers

I'm afraid Cos's answer isn't quite accurate. You don't have to wire it up because the base HttpApplication class does it for you. There isn't an interface or an override here; HttpApplication uses reflection to hook up these methods to the events based on the method name. So it's a bit of convention-based magic that has been in the framework for a while. It's very strange, and I think they only did it to maintain similarities with Classic ASP back in the day and/or provide a shortcut to avoid writing lots of small HttpModules.

For the curious, load up HttpApplication in Reflector and hunt for the HookupEventHandlersForApplicationAndModules() method. Or, load HttpApplicationFactory and look at the ReflectOnApplicationType() and ReflectOnMethodInfoIfItLooksLikeEventHandler() (love that name!) methods.

Rick Strahl discussed this on his blog a few years ago.

Is it something you really need to know to use ASP.NET? No, but knowing it certainly removes some of the mystery.

like image 124
Nicholas Piasecki Avatar answered Sep 19 '22 20:09

Nicholas Piasecki


Any ASP.NET application is an object (or class) of type :

public class Global : System.Web.HttpApplication (you will find this in the global.asax)

The ASP.NET engine invoke by IIS creates an instance of your object and the HttpApplication interface demands Application_BeginRequest, which is invoke by IIS (by way of the ISAPI)

When the ASP.NET Engine creates an instance of your class it looks like this:

HttpApplication thisAspApp = new YourASPApplication() thisApplication.Begin_Request() 

Because it casts your app as a derived type, the known interface can be directly accessed without need for overrides. While HttpApplication is a class it is being used as an interface by way of casting. If you add a new method (or property) to your class the ASP.NET engine can not access that method because it is only aware of your application as a generic HttpApplication. In VS if you go to the global.asax and right click over HttpApplication in the class declaration and select "Go To Definition" (or press F12) you can see the structure of base class. (or you can find it in MSDN online).

like image 35
Cos Callis Avatar answered Sep 18 '22 20:09

Cos Callis