在 Razor 视图中引用 JavaScript 文件以获取 JavaScript Intellisense

发布于 2024-10-02 09:12:55 字数 269 浏览 1 评论 0原文

Visual Studio 提供 JavaScript Intellisense。它足够聪明,可以看到您引用母版页中的 JavaScript 文件(例如 jQuery 文件),然后在应用程序的任何视图中提供语句完成。然而,这似乎不适用于 Razor。有没有办法让它与 Razor 一起工作? ASPX 视图引擎提供了这个技巧,例如: <% /* %><% */%>

Visual Studio offers JavaScript Intellisense. It's smart enough to see that you refer to JavaScript files in your master pages (for example jQuery file) and then offers statement completion in any view of the application. However this doesn't seem to work with Razor. Is there a way to get this working with Razor?
ASPX view engine offers this trick for example: <% /* %><script src="~/Scripts/jquery-1.4.1-vsdoc.js"></script><% */ %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月牙弯弯 2024-10-09 09:12:55

您应该能够执行以下操作:

@if (false) {
<script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
}

这样,当应用程序运行时,代码将永远不会运行,但 VS 不会知道 if (false),因此它将解析

@* <script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> *@

You should be able to do something like this:

@if (false) {
<script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
}

That way the code will never run when the app runs, but VS won't know about the if (false), so it will parse the <script> tag and allow Intellisense to take it into account. The problem with using Razor comments in Razor files is that VS will recognize them and completely ignore anything inside them. For example, this won't work:

@* <script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> *@
寄与心 2024-10-09 09:12:55

为了防止编译器发出有关无法访问代码的警告,您可以使用编译指示进一步包装它:

@{ #pragma warning disable }
@if (false) 
{ 
    <script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> 
} 
@{ #pragma warning restore } 

To prevent compiler warnings about unreachable code, you can further wrap this with a pragma:

@{ #pragma warning disable }
@if (false) 
{ 
    <script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> 
} 
@{ #pragma warning restore } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文