[英]Getting jquery to work with MVC4
It's not happening. 没发生 I tried linking the scripts normally in _Layout.cshtml with
<script src='@Content.Url("...
, installing Web Optimization and using bundles, and putting @Scripts.Render()
in the head/body/bottom of _Layout.cshtml and before the <script>
tag on the relevant page. What am I missing? 我尝试将_Layout.cshtml中的脚本通常与
<script src='@Content.Url("...
链接起来,安装Web优化并使用捆绑包,然后将@Scripts.Render()
放在_Layout的头部/正文/底部.cshtml以及相关页面上<script>
标记之前,我缺少什么?
Directory structure 目录结构
Views
Shared
Js
jquery-1.10.2.js
jquery.color-2.1.2.min.js
BundleConfig.cs BundleConfig.cs
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Views/Shared/Js/jquery-{version}.js",
"~/Views/Shared/Js/jquery.color-{version}.js")
);
}
Global.asax.cs Global.asax.cs
protected void Application_Start()
{
// blah, etc, then:
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
_Layout.cshtml _Layout.cshtml
@Scripts.Render("~/bundles/jquery");
I can run the actual javascript/jquery code on JsFiddle, so the validity of what I've written there isn't the problem. 我可以在JsFiddle上运行实际的javascript / jquery代码,所以我写的内容的有效性没有问题。
BundleCollection
class has IgnoreList
property, which contains the patterns of files' names, that will not be rendered. BundleCollection
类具有IgnoreList
属性,该属性包含将不呈现的文件名模式。 By default it ignores names with min
word. 默认情况下,它将忽略带有
min
字词的名称。 You can clear this list inside your RegisterBundles
: 您可以在
RegisterBundles
清除此列表:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.IgnoreList.Clear();
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Views/Shared/Js/jquery-{version}.js",
"~/Views/Shared/Js/jquery.color-{version}.js")
);
}
Got it working: all I did was move the scripts to a different folder, Content/Js
, and linked it in via <script src='@Content.Url("...
. Content
is on the same directory level as Views
. 正常工作:我所做的就是将脚本移到另一个文件夹
Content/Js
,并通过<script src='@Content.Url("...
链接了它。” Content
与Views
在同一目录级别。
All is well now except that I have no idea why the folder matters. 现在一切都很好,除了我不知道为什么文件夹很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.