简体   繁体   English

"Visual Studio 2017 中的 Javascript 断点"

[英]Javascript breakpoints in Visual Studio 2017

I just installed Visual Studio 2017. After starting an ASP.NET MVC application I get the message that chrome debugging in Visual Studio is enabled.我刚刚安装了 Visual Studio 2017。启动 ASP.NET MVC 应用程序后,我收到消息,指出 Visual Studio 中的 chrome 调试已启用。

But my breakpoints in Visual Studio won't hit.但是我在 Visual Studio 中的断点不会命中。 The breakpoints on Razor code seems to be working but Javascript does not. Razor 代码上的断点似乎可以正常工作,但 Javascript 不能。 The breakpoints are not fully red like they should be.断点没有像应有的那样完全变红。 A restart, rebuild don't seem to take any effect.重新启动,重建似乎没有任何效果。

I have the following code example我有以下代码示例

@Html.Partial("_Test", Model.Test) // debugging works here

<script>
    var i = 1;
    console.log(i); // debugging does not work here or above
</script>

@section scripts {
    <script>
        var a = 11;
        console.log(a); // debugging does not work here or above
    </script>
}

This feature does not work for javascript code inside a *.cshtml file, but only for code in separate *.js (or *.ts) files.此功能不适用于 *.cshtml 文件中的 javascript 代码,但仅适用于单独的 *.js(或 *.ts)文件中的代码。

在此处输入图片说明 Notice how the breakpoint in the JavaScript file is active, while the breakpoint in the Razor view is not.请注意 JavaScript 文件中的断点如何处于活动状态,而 Razor 视图中的断点未处于活动状态。

I have also noticed that breakpoints will not be hit for JavaScript files when JavaScript code is executed during page load when the page is loaded the first time in the current Chrome session.我还注意到,当页面在当前 Chrome 会话中第一次加载时,在页面加载期间执行 JavaScript 代码时,不会为 JavaScript 文件命中断点。 Breakpoints will only work after the document has finished loading once.断点仅在文档完成加载一次后起作用。

Another tip: js debug only can be enabled after setting 'start URL' on run/debug.另一个提示:只有在运行/调试时设置“开始 URL”后才能启用 js 调试。

Only in this way VS will attach to browser process.只有这样 VS 才会附加到浏览器进程。

I had the same issue.我遇到过同样的问题。 I followed the instructions from the same post: https://blogs.msdn.microsoft.com/webdev/2016/11/21/client-side-debugging-of-asp-net-projects-in-google-chrome/我按照同一帖子中的说明进行操作: https : //blogs.msdn.microsoft.com/webdev/2016/11/21/client-side-debugging-of-asp-net-projects-in-google-chrome/

I did the following in Visual Studio: Go to Tools -> Options -> Debugging -> General and turned off the setting "Enable JavaScript Debugging for ASP.NET (Chrome and IE)".我在 Visual Studio 中执行了以下操作:转到Tools -> Options -> Debugging -> General并关闭设置“为 ASP.NET(Chrome 和 IE)启用 JavaScript 调试”。 Saved changes, then run the webpage.保存更改,然后运行网页。

After this, I noticed that Chrome was not showing the "Debug webpage" before loading my page.在此之后,我注意到 Chrome 在加载我的页面之前没有显示“调试网页”。 Then i closed my webpage and stopped the debugging session.然后我关闭了我的网页并停止了调试会话。

I re-enabled the "Enable Javascript Debugging for ASP.NET (Chrome and IE)" option, then run the webpage using IE, and it worked.我重新启用了“为 ASP.NET(Chrome 和 IE)启用 Javascript 调试”选项,然后使用 IE 运行该网页,它工作正常。 The page stopped and the breakpoint showed up in my visual studio.页面停止,断点出现在我的视觉工作室中。

I had the same issue, new to .net core, a solution that worked for me was to add debugger;我有同样的问题,刚接触 .net 核心,对我有用的解决方案是添加调试器; at the top of the script:在脚本的顶部:

<script>
  debugger;

  -- rest of your jscript here
</script>

It does work in vs 2017 only with enabling option "Enable Javascript Debugging for ASP.NET (Chrome and IE)" and setting debugger;只有启用选项“启用 ASP.NET(Chrome 和 IE)的 Javascript 调试”并设置调试器,它才能在 vs 2017 中工作; in js code in *.cshtml as well as *.js file在 *.cshtml 以及 *.js 文件中的 js 代码中

After thinking the only way to get this working was to do a repair on Visual Studio 2017, using the installer, I realized that we had BundleTable.EnableOptimizations = true;在考虑让这个工作的唯一方法是使用安装程序在 Visual Studio 2017 上进行修复后,我意识到我们有BundleTable.EnableOptimizations = true; in Bundle.config .Bundle.config I removed this line and added it to Web.Debug.config transformation and it started working as expected.我删除了这一行并将其添加到Web.Debug.config转换中,它开始按预期工作。

Two options you could try:您可以尝试两种选择:

  1. Turn off the "Enable JavaScript debugging for ASP.NET (Chrome and IE)" in VS options, and then use the debugger (F12) in your browser.在 VS 选项中关闭“为 ASP.NET(Chrome 和 IE)启用 JavaScript 调试”,然后在浏览器中使用调试器(F12)。
  2. Start debugging in Visual Studio and then add your breakpoint(s) in Solution Explorer -> Internet Explorer -> <your view name> .在 Visual Studio 中开始调试,然后在Solution Explorer -> Internet Explorer -> <your view name>添加断点。 (Assumes you are using IE) (假设您使用的是 IE)

My best guess here (at least in my case, potentially in your case as well) is that it has to do with bundling.我在这里最好的猜测(至少在我的情况下,也可能在你的情况下)是它与捆绑有关。 The project I'm working on right now bundles separate scripts into one Big Daddy script (without minifying in debug configuration) so my hunch is that VS won't map my breakpoint to that ultimate script run in the browser.我现在正在处理的项目将单独的脚本捆绑到一个 Big Daddy 脚本中(在调试配置中没有缩小),所以我的预感是 VS 不会将我的断点映射到在浏览器中运行的最终脚本。

I know this doesn't answer the question since it's just a hunch, but I'm hoping it points us in the right direction.我知道这并不能回答问题,因为这只是一种预感,但我希望它为我们指明了正确的方向。 Maybe if we can validate this against others' experience this could evolve into a real answer.也许如果我们可以根据其他人的经验验证这一点,这可能会演变成一个真正的答案。

For those who use Google Chrome Portable (or added chrome to browsers debug list manually) helps this:对于那些使用 Google Chrome Portable(或手动将 chrome 添加到浏览器调试列表)的人来说,这有帮助:

https://docs.microsoft.com/en-us/visualstudio/javascript/tutorial-nodejs-with-react-and-jsx?view=vs-2017#set-and-hit-a-breakpoint-in-the-client-side-react-code https://docs.microsoft.com/en-us/visualstudio/javascript/tutorial-nodejs-with-react-and-jsx?view=vs-2017#set-and-hit-a-breakpoint-in-the-客户端反应代码

You need to attach then to proper chrome instance manually and viola您需要手动附加到正确的 chrome 实例和中提琴

  • Visual Studio Professional 2017 Version 15.8.1 Visual Studio Professional 2017 版本 15.8.1
  • Chrome Version 68.0.3440.106 (Official Build) (64-bit) Chrome 版本 68.0.3440.106(官方版本)(64 位)

I had to enable Legacy Chrome JavaScript debugger under:我必须在以下位置启用旧版 Chrome JavaScript 调试器:

Options -> Tools -> Debugging -> General -> Enable legacy Chrome JavaScript debugger for ASP.NET选项 -> 工具 -> 调试 -> 常规 -> 为 ASP.NET 启用旧版 Chrome JavaScript 调试器

I finally got all my break points to begin hitting after I right clicked on IIS Express and Exit after working for many hours.在我工作了几个小时后右键单击 IIS Express 并退出后,我终于得到了所有断点开始命中。

I had debugger in my script files and break points and as soon as I did a Clean Solution > Debug my breakpoints started to begin to hit again.我的脚本文件和断点中有调试器,只要我执行了 Clean Solution > Debug 我的断点就开始再次命中。

I understand how frustrating this is and I am just letting the stackoverflow community know what worked for me.我明白这是多么令人沮丧,我只是让 stackoverflow 社区知道什么对我有用。

我遇到了同样的情况 Javascript 断点没有命中,然后我卸载并重新安装了 Visual Studio 2017,它又开始工作了

I had the same situation Javascript break point were not hitting and then i uninstalled and reinstall visual studio 2017 but it dosent work.我有同样的情况 Javascript 断点没有命中,然后我卸载并重新安装了 Visual Studio 2017,但它不起作用。 Re install Windows 10 no solution.重新安装Windows 10 没有解决办法。 Than finally I re-educated computer science.最后,我重新接受了计算机科学的教育。 Now everythigng is ok.现在一切正常。

Thank you vs2017, thanks all of microsoft.谢谢vs2017,感谢所有微软。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM