BeginRequest runs static files in an ASP.NET MVC application

I had the impression that static files (CSS, images, @ font-face files, etc.) completely bypassed ASP.NET and were served directly by IIS.

However, the BeginRequest event handler is called for each HTTP request, including for static files. This concerns me because I am creating an Entity Framework data context that will be used for the life of each request in this event handler. I do not want to create these contexts if they will never be used.

I am using IIS 7 in Windows 7 Ultimate without specific handler definitions. Is it wrong with me? Should these events shoot?

+5
source share
4 answers

I believe that the ASP.NET MVC site by default has this set in the web.config file.

<modules runAllManagedModulesForAllRequests="true" />  

This means that every .NET module will be loaded for every IIS request. This is necessary for ASP.NET MVC to handle routing without extension. This is essentially a wildcard mapping that you would write in IIS, which would match everyone and direct it to ASP.NET, which lives in web.config.

More details here , including a way to disable behavior if you are not using .NET 4.0. This is nasty, but it is the cleanest solution for sites that cannot handle the overhead of having static files served by asp.net.

+6
source

BeginRequest will be launched for all requests (including static content) if:

  • - Visual Studio.
  • IIS .

, : http://forums.asp.net/t/1220664.aspx

+1

Lazy initialization Lazy<T> ObjectContext: http://msdn.microsoft.com/en-us/library/dd997286.aspx

+1

IIS 7 , .

.

. , .

0
source

All Articles