asp.net-mvc Visual Studio 不允许在 MVC 视图中使用断点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18495698/
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-08 03:08:00  来源:igfitidea点击:

Visual Studio does not allow breakpoints in MVC views

asp.net-mvcvisual-studiodebuggingvisual-studio-2012breakpoints

提问by levininja

Sometimes Visual Studio does not allow me to set breakpoints in MVC views. This has happened to me scores of times, but it doesn't happen for every view and I don't know why.

有时 Visual Studio 不允许我在 MVC 视图中设置断点。这在我身上发生了很多次,但并不是每次视图都会发生,我不知道为什么。

When you click on the left-hand bar to place a breakpoint, it places a white circle instead of the normal red circle. The message when you hover over it is "The breakpoint will not currently be hit. The source code is different from the original version." It goes on to describe how to allow breakpoints to be hit, but that produces strange results and I don't want that anyways.

当您单击左侧栏以放置断点时,它会放置一个白色圆圈而不是普通的红色圆圈。将鼠标悬停在其上时会显示“当前不会命中断点。源代码与原始版本不同。” 它继续描述如何允许断点被击中,但这会产生奇怪的结果,无论如何我都不想要。

If the error is correct, then I want to run the original source code. I don't know what's going on behind the scenes in VS; I try rebuilding and all that but it doesn't help. I'm running in Debug mode in VS 2012.

如果错误是正确的,那么我想运行原始源代码。不知道VS的幕后情况;我尝试重建和所有这些,但它没有帮助。我在 VS 2012 中以调试模式运行。

screenshot of trying to place a breakpoint in vs 2012 MVC razor view

尝试在 vs 2012 MVC razor 视图中放置断点的屏幕截图

回答by Kirk Broadhurst

This could be caused by many things, but a few items to check which I've helped people with recently:

这可能是由很多事情引起的,但有一些项目需要检查我最近帮助过的人:

  • First step: there must be a PDB file alongside the DLL to enable debugging. (see: What is a PDB file?) Make sure you have the PDB in the executing directory.

  • Clean to remove all the old DLLs from your bin folders.

  • Ensure that your application is running a build of your current code (the same version you have in Visual Studio). Don't assume that it is just because you clicked 'build' or 'deploy'. If no changes were detected then things often don't happen. Check the build time of the assembly, or change something and rebuild to see the file size change.

  • If you're running something web-related, make sure a browser isn't caching code, or IIS isn't holding a long running process.

  • Kill any running Visual Studio Development Server instances (you can do this from task manager, or more simply from the system tray - they look like an IE logo and when you hover over them they'll tell you which port they run on).

  • Restart IIS using iisresetfrom a command prompt.

  • Check the settings for Debugging in Visual Studio (Tools > Options > Debugging > Symbols) You want to automatically load symbols, and if you're linking other assemblies you need to reference their PDB files here.

  • 第一步:DLL 旁边必须有一个 PDB 文件以启用调试。(请参阅:什么是 PDB 文件?)确保在执行目录中有 PDB。

  • 清除以从 bin 文件夹中删除所有旧的 DLL。

  • 确保您的应用程序正在运行当前代码的构建(与 Visual Studio 中的版本相同)。不要以为这只是因为您单击了“构建”或“部署”。如果没有检测到任何变化,那么事情通常不会发生。检查程序集的构建时间,或更改某些内容并重新构建以查看文件大小的变化。

  • 如果您正在运行与 Web 相关的内容,请确保浏览器没有缓存代码,或者 IIS 没有保持长时间运行的进程。

  • 杀死任何正在运行的 Visual Studio Development Server 实例(您可以从任务管理器中执行此操作,或者更简单地从系统托盘中执行此操作 - 它们看起来像一个 IE 徽标,当您将鼠标悬停在它们上方时,它们会告诉您它们在哪个端口上运行)。

  • iisreset从命令提示符使用重新启动 IIS 。

  • 检查 Visual Studio 中调试的设置 ( Tools > Options > Debugging > Symbols) 您想自动加载符号,如果您要链接其他程序集,则需要在此处引用它们的 PDB 文件。

回答by Bryan Halterman

So I had this issue this morning and the fix for me was related to razor syntax.

所以今天早上我遇到了这个问题,我的修复与剃刀语法有关。

I was setting a variable inside an if statement

我在 if 语句中设置了一个变量

@If (my condition)
{
  myVar1 = "blah blah blah"
  @myVar2 = 1 <== This line here was causing my razor to crap out on render
}

So all of the other things are good things however, improper razor syntax can also cause the breakpoint issue. In this case it was the @ symbol on myVar2 inside the code block... Just an FYI

因此,所有其他事情都是好事,但是,不正确的 razor 语法也会导致断点问题。在这种情况下,它是代码块内 myVar2 上的 @ 符号......仅供参考

回答by Casper Kvolsb?k

enter image description here

在此处输入图片说明

Make sure that "debug" is set and Voila ... debug is working again:-)

确保设置了“调试”并且瞧...调试再次工作:-)

回答by levininja

The simplest solution I've found to work around this problem is:

我发现解决这个问题的最简单的解决方案是:

Set a breakpoint in the controller code that is right before the View is called. Then, when that breakpoint is hit, step through (using F10) several times. It will go through _ViewStart.cshtml and maybe another thing or two. But soon it will get to the view.

在调用 View 之前的控制器代码中设置断点。然后,当该断点被击中时,单步执行(使用 F10)几次。它将通过 _ViewStart.cshtml,也许还有一两件事。但很快它就会进入视野。

Once you are in the view, then hitting F5 (continue) will take you to the breakpoint in the view.

进入视图后,按 F5(继续)将带您到视图中的断点。

回答by Pete

Fom the answer nothing worked for me to set a breakpoint in the javascript code. I moved the javascript code inside to file Scripts\myscript.js and replaced the script block to

从答案来看,在 javascript 代码中设置断点对我没有任何帮助。我将里面的 javascript 代码移动到文件 Scripts\myscript.js 并将脚本块替换为

<script src="@Url.Content("~/Scripts/myscript.js")"></script>

回答by Aaron Hudon

To add to @kirk-broadhurst answer, (please amend if possible), double check your web.config, specifically the compilationflag under system.web. Even if you are building for debug, if the debugattribute is set to false, you will run into issues debugging Razor.

要添加到@kirk-broadhurst 答案中(如果可能,请修改),请仔细检查您的 web.config,特别是compilationsystem.web 下的标志。即使您是为调试而构建,如果该debug属性设置为 false,您也会在调试 Razor 时遇到问题。

<system.web>
    <compilation debug="true" targetFramework="4.6" />
</system.web>

回答by Joe

Make sure your Solution configuration set to Debug not on release.

确保您的解决方案配置设置为 Debug not on release。

Thanks

谢谢