asp.net-mvc 为什么在调试 ASP.NET MVC 应用程序时不触发 Application_Start() 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/967813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:38:13  来源:igfitidea点击:

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

asp.net-mvcdebuggingcompilation

提问by Tomas Aschan

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

我目前在我的Global.asax.cs文件中有以下例程:

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.aspxin 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.

当我点击 时F5,应用程序会启动,除非我Index.aspx~/Views/Home/文件夹中指定了一个视图,否则我会收到“缺少视图”的错误消息,尽管我已经重新定义了默认路由并删除了HomeController. 我希望获得路由调试器,如果没有,至少需要一个~/Views/Arrangement/Index.aspx. 调试时永远不会命中
断点RegisterRoutes(Routetable.Routes);

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?

我尝试过构建、重建、重新启动 VS、清理、再次重建等,但似乎没有任何效果。为什么应用程序不运行当前版本的代码?

采纳答案by Tomas Aschan

I found the problem:

我发现了问题:

This MVC application was part of a larger solution, in which I had at one point set another project to build for an x86 environment (I'm running x64). When I did that, apparently all other projects - even those added later - were set not to build on Ctrl+Shift+B, and I suppose that's why the debugger didn't hit my breakpoint.

这个 MVC 应用程序是一个更大的解决方案的一部分,我曾经在其中设置了另一个项目来为 x86 环境构建(我正在运行 x64)。当我这样做时,显然所有其他项目 - 甚至那些后来添加的 - 都设置为不构建在 上Ctrl+Shift+B,我想这就是调试器没有达到我的断点的原因。

Solution:

解决方案:

Go into the solution build properties (right-click Solution, select properties, and select Build on the menu on the left), and put a check in the Build checkbox next to the project name in the list.

进入解决方案构建属性(右键单击解决方案,选择属性,然后在左侧菜单上选择构建),并选中列表中项目名称旁边的构建复选框。

回答by Vishal Seth

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

在 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.

您是使用 IIS7 作为服务器还是内置 Web 服务器?我注意到在使用 IIS7 时,如果你启动调试器,让一个页面出现,然后在调试器仍在运行时更改 Global.asax(标记文件,而不是代码隐藏),然后刷新页面,Application_Start 中的断点将是打。

我认为发生的事情是按下“播放”,VS 只是启动进程,然后附加到它,但是当它附加到它时,开始事件已经运行。通过更改 Global.asax,您会导致应用程序重新启动,并且由于调试器已经附加,您可以点击断点。不是一个很好的解决方案,但它似乎有效。

Thats's what was happening in my case.

这就是我的情况。

回答by JotaBe

Add a System.Diagnostics.Debugger.Break();to Application_Start().
This will force a breakpoint.

添加一个System.Diagnostics.Debugger.Break();Application_Start().
这将强制断点。

This line should be commented out to avoid the breakpoint to happern and #ifdef debugto get sure never gets to production.

应该注释掉这一行以避免断点发生并#ifdef debug确保永远不会进入生产。

回答by John Rasch

I believe you have to shutdown/stop the local debugging server in order for the Application_Start()event to fire again... you should be able to right click on it in the system tray and choose "Stop".

我相信您必须关闭/停止本地调试服务器才能Application_Start()再次触发事件......您应该能够在系统托盘中右键单击它并选择“停止”。

回答by Paul Totzke

The problem is Application_Start()triggers first then the debugger attaches.

问题是Application_Start()首先触发然后调试器附加。

So the goal is to do something that would cause Application_Start()to trigger again while its still running. For debugging purposes, just run the debugger like you normally do then edit (eg add a newline) and save the web.config file.

所以我们的目标是做一些会导致Application_Start()在它仍在运行时再次触发的事情。出于调试目的,只需像平常一样运行调试器,然后编辑(例如添加换行符)并保存 web.config 文件。

回答by Dev

I hit with the same problem today and was using Local IIS.

我今天遇到了同样的问题,正在使用本地 IIS。

I believe this arises because because of the way page is loaded. All you need to do is, put a breakpoint in Application_Start. Run the application(In my case, it took to Login Screen ) and then go to web.config. Edit it - by adding some spaces. and then refresh in Browser.that will hit the breakpoint on Application_Start.

我相信这是因为页面加载的方式。您需要做的就是在 Application_Start 中放置一个断点。运行应用程序(在我的情况下,它需要登录屏幕),然后转到 web.config。编辑它 - 通过添加一些空格。然后在 Browser.that 中刷新,这将在 Application_Start 上遇到断点。

回答by Hernaldo Gonzalez

In my case was I using

在我的情况下,我使用

Local IIS Web serverin web project settings

Web 项目设置中的本地 IIS Web 服务器

and I change to Use Visual Studio Development Server, and this works.

我更改为Use Visual Studio Development Server,这有效。

回答by Rupesh Kumar Tiwari

Below technique worked for me:
Simple workaround is to touch global.asax after the debugger is attached in order to force an application recycle. Then during the next request, the break point that you set on Application_Start will be hit.

以下技术对我
有用:简单的解决方法是在附加调试器后触摸 global.asax 以强制应用程序回收。然后在下一个请求期间,您在 Application_Start 上设置的断点将被命中。

I found this here:
http://connect.microsoft.com/VisualStudio/feedback/details/634919/cannot-debug-application-start-event-in-global-asax

我在这里找到了这个:http:
//connect.microsoft.com/VisualStudio/feedback/details/634919/cannot-debug-application-start-event-in-global-asax

回答by Ondrej Svejdar

In my case the problem was between chair and keyboard - after renaming mvc project assembly, I forgot to update Global.asax to point to the correct class. So check the "Inherits" in Global.asax (in visual studio right-click on Global.asax -> View Markup)

就我而言,问题出在椅子和键盘之间 - 在重命名 mvc 项目程序集后,我忘记更新 Global.asax 以指向正确的类。因此,请检查 Global.asax 中的“继承”(在 Visual Studio 中右键单击 Global.asax -> 查看标记)

<%@ Application Codebehind="Global.asax.cs" 
  Inherits="Monster.MgsMvc.Web.MvcApplication" 
  Language="C#" %>

if it really matches your Global.asax.cs class/namespace declaration

如果它真的匹配您的 Global.asax.cs 类/命名空间声明

回答by Avishay

Maybe my solution will help someone:

也许我的解决方案会帮助某人:

  1. Stop the debugger.
  2. Update the RegisterRoutescode (example - add\remove int i = 1;).
  3. Rebuild.
  4. Place a break point in RegisterRoutes.
  1. 停止调试器。
  2. 更新RegisterRoutes代码(例如 - add\remove int i = 1;)。
  3. 重建。
  4. 中放置一个断点RegisterRoutes