Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio does not allow breakpoints in MVC views

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.

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.

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

like image 660
levininja Avatar asked Aug 28 '13 18:08

levininja


People also ask

Why breakpoints are not working on Visual Studio?

First Close Visual Studio, Open Project again in Visual Studio -> Clean Solution, and then Rebuild Solution. Make sure, you have set the configuration to "Debug" mode and you are not working on "Release" mode while debugging your application. Click "Ok" and re-build your project.

How do I enable breakpoints in Visual Studio?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

Can you use breakpoints in release mode?

You can then put a breakpoint on that int declaration line, and it'll be hit, even in release mode. Then just step up the stack in the debugger back to the function you actually wanted a breakpoint in.


2 Answers

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.

like image 122
Kirk Broadhurst Avatar answered Oct 07 '22 00:10

Kirk Broadhurst


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

like image 38
Bryan Halterman Avatar answered Oct 07 '22 02:10

Bryan Halterman