Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does not the Application_Start() event fire when I debug my ASP.NET MVC app?

I currently have the following routines in my Global.asax.cs file:

public static void RegisterRoutes(RouteCollection routes) {     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");     routes.MapRoute(         "Default",                                                   "{controller}/{action}/{id}",                                new { controller = "Arrangement", action = "Index", id = "" }     ); }  protected void Application_Start() {     RegisterRoutes(RouteTable.Routes);     // Debugs the routes with Phil Haacks routing debugger (link below)     RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); } 

Routing debugger...

When I hit F5, the application fires up and unless I have a view named Index.aspx in the ~/Views/Home/ folder, I get the "View missing" error message, although I have re-defined the default route and removed the HomeController. I would expect to get the routing debugger, and if not that at least a request for ~/Views/Arrangement/Index.aspx.
A breakpoint on RegisterRoutes(Routetable.Routes); is never hit when debugging.

I have tried building, rebuilding, restarting VS, cleaning, rebuilding again etc, but nothing seems to work. Why doesn't the application run the current version of the code?

like image 211
Tomas Aschan Avatar asked Jun 09 '09 01:06

Tomas Aschan


People also ask

How to debug Application_ Start?

Easiest Solution: Use the Visual Studio Web Server or IIS Express. Now if you press F5 to debug from within Visual Studio the debugger happily triggers on your breakpoint. This works because Visual Studio actually launches the local server EXE manually and so can easily attach the debugger on startup.

What is Application_Start?

Application_Start. Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values.

What is Application_Start in global ASAX?

Application_Start. The Application_Start event is fired the first time when an application starts. Session_Start. The Session_Start event is fired the first time when a user's session is started. This typically contains for session initialization logic code.


1 Answers

I found the following answer on forums.asp.net:

Are you using IIS7 as the server or the built-in web server? I noticed when using IIS7 that if you start the debugger, let a page come up, then change Global.asax (the markup file, not code-behind) while the debugger is still running, then refresh the page, breakpoints in Application_Start will be hit.

I think what's happening is that pressing "play", VS just fires up the process, then attaches to it, but by the time it attaches to it the start event has already run. By changing Global.asax, you cause the app to restart and since the debugger's already attached you can hit the breakpoint. Not a great solution, but it seems to work.

Thats's what was happening in my case.

like image 152
Vishal Seth Avatar answered Sep 20 '22 14:09

Vishal Seth