Visual Studio does not allow breakpoints in MVC views

吃可爱长大的小学妹 提交于 2019-11-29 10:11:25
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 iisreset from 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.

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.

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

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 (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

Casper Kvolsbæk

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

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

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

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

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

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

Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!