通过CDN为Bootstrap提供Visual Studio智能感知

25

我已经通过Microsoft CDN安装了Bootstrap:

<head>
    ...
   <link href="//ajax.aspnetcdn.com/ajax/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
   <link href="//ajax.aspnetcdn.com/ajax/bootstrap/3.1.1/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />
</head>

<body>
    ...
    <script src="//ajax.aspnetcdn.com/ajax/bootstrap/3.1.1/bootstrap.min.js"></script>
</body>

Bootstrap的一切都正常,但是Visual Studio没有为类提供任何智能感知。

例如,输入

<div class="(intellisense should open here)

没有任何结果显示。

是否有办法从CDN获取智能感知?


Visual Studio 的哪个版本? - Ravimallya
@Scottie,你最终解决了这个问题吗?我这边也在处理同样的事情。使用VS 2013 Ultimate MVC。CSS文件被引用在_Layout.cshtml文件中... - IEnjoyEatingVegetables
2
不,我从来没有这样做过。我只是在我的解决方案中有.css文件,Intellisense会捕捉到它们,即使我从未使用过它们。 - Scottie
10个回答

33

如果你正在使用ASP.NET Core项目,请按照以下步骤操作。VS仅引用wwwroot文件夹中的文件。 你需要打开.csproj文件并添加以下代码段。

<ItemGroup>
  <None Include="node_modules/**" />
</ItemGroup>

之后,Visual Studio将从node_modules中获取智能感知。


1
非常好用!虽然我让我的包含路径特定于引导路径,因为有很多节点模块我不想看到。 - Jay
谢谢 - 这让我感到很疯狂。在 VS 2019 16.3 中确认。 - Christopher
太好了! 谢谢。 - user1309226
1
可以确认在 VS 2019 Razor 项目中仍然有效。我引用了 <None Include="node_modules\tailwindcss\dist\*.css" /> 来获取对 TailwindCSS 的支持。 - GoClimbColorado

11
你可以为JavaScript添加CDN引用,但不支持CSS。你可以将文件添加到项目中,它会起作用,你不需要添加html引用,只需要添加到项目中即可解决问题。

你能在这里使用{version}吗? - Scottie
7
在 Visual Studio 2017 中,JavaScript\IntelliSense\References 节点不存在。即使在上述路径中将 JavaScript 翻译为 JavaScript/TypeScript,也找不到 References 节点。我认为微软已经删除了它。 - Zarepheth

5
您需要通过NuGet Packages安装Bootstrap。之后,Intellisense将开始工作。

1

我刚刚安装了Bootstrap NuGet。


我必须将软件包从3.4.1更新到5.0.1。 - Jeff Blumenthal

1
打开Visual Studio
进入扩展和更新,然后点击在线选项卡。
在搜索框中输入Bootstrap
安装以下包以启用智能感知。确保在尝试插入代码片段之前已将bootstrap.css(或bootstrap.min.css和js文件)拖放到项目中。
  • Bootstrap Bundle
  • Bootstrap Snippet Pack

enter image description here


1
这个解决方案立即修复了我的智能感知问题。具体来说,是 Bootstrap Bundle 包。 - FrankO
1
Bootstrap Bundle没有适用于VS 2017的版本。 - BeanFlicker

0

我知道这种方式不完全是CDN,但它对我有用,而且你仍然可以保持你的包最新

你可以使用Bower引用Bootstrap。

在package.json中,你应该有:

{
  "version": "1.0.0",
  "name": "TaskAngularJSApp",
  "private": true,
  "devDependencies": {
    "grunt": "0.4.5",
    "grunt-contrib-uglify": "0.7.0",
    "grunt-contrib-watch": "0.6.1",
    "bower": "1.7.3"
  }
}

然后你会有一个类似这样的bower.json文件:

{
    "name": "ASP.NET",
    "private": true,
    "dependencies": {
        "bootstrap": "*"
    }
}

然后在你的HTML中,你可以像这样引用Bootstrap:

    <link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.min.css" />
    <script src="/lib/bootstrap/dist/js/bootstrap.min.js"></script>

我得到了帮助: 使用grunt bower gulp npm与Visual Studio 2015一起为asp-net-4-5项目今天发布:Visual Studio 2015、ASP.NET 4.6、ASP.NET 5和EF 7预览版 - 在“HTML编辑器更新”部分。

0

工具->选项->文本编辑器->特定语言->智能感知

这应该可以让智能感知重新启动并运行,之前对我有用过(我想这就是你要找的东西)。


我知道你回答了"...-> 具体语言 ->IntelliSense",但是CSS似乎没有IntelliSense的替代选项? - Hauns TM
你使用的是哪个版本的VS?2012?2013? - user4157770
我有Visual Studio 2013。 - Hauns TM
嗨Hauns,2012年和2013年之间有一个变化,这是我能提供的最好答案,点击链接:link - 希望能帮到你! - user4157770

0

前往工具->选项->文本编辑器->HTML->高级->Razor-> 选择True

enter image description here


0

0
Content 文件夹中复制 bootstrap.css

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接