How can I get Javascript debugging to work with my ASP.NET MVC application?

19,476

Solution 1

function test()
{
  debugger;
  alert("hi");
}

I don't know if it will work with express, but when IE hits the "debugger" instruction, it asks me if I want to debug, and with wich tool. Visual studio is present in the list of options presented to me.

I don't know if express will be present for you though.

"debugger" also works with firebug.

Solution 2

I just had this problem today as well. The debugger doesn't know where you script files are if you include them in the mvc view.

Put them in an external .js file (say default.js) in the scripts folder, add a script tag in the site.master and then you will hit your breakpoints.

Solution 3

VS JS debugging can work, but... Honestly, get Firebug. It's free, and does much, much more than the VS debugger.

Solution 4

Here's my workaround for debugging javascript in MVC. VS2008 doesn't pick up on break points for javascript in an aspx page, but it will for a separate .js file. I create a debugJscript.js file when I'm working on a page. I create a reference to that debug page from my aspx page. I can step through javascript that way and then push the javascript code back to the aspx page when I'm happy with it.

You can avoid caching problems, too, on that external .js file if you refer to your file like so:

    <script type="text/javascript" src="../../Scripts/DebugJScript.js?<%=DateTime.Now %>" ></script>    
Share:
19,476
Mr. Kraus
Author by

Mr. Kraus

It's personal

Updated on June 24, 2022

Comments

  • Mr. Kraus
    Mr. Kraus almost 2 years

    I can't seem to get Javascript debugging working for my ASP.NET MVC application even though I can for a traditional ASP.NET WebForm app.

    I have followed the steps for unchecking the 'Disable Script Debugging' boxes for both IE and other.

    when I add a simple function to display an alert in both the site.master and any content view the breakpoint will not fire.

    Have i missed something obvious or do I need to use an outside tool for debugging like FireBug?

    By the way, I'm using Visual Studio Web Developer Express 2008.

    thx

  • Mr. Kraus
    Mr. Kraus over 15 years
    I don't think it's Express, as pointed out in the post, Javascript debugging works in WebForm app in Express, just not MVC.