[英]Why do i have to include JS files in order to work with jquery in every single view?
As the question says all about it but to give you a little more information i included all the .js
files in my Asp.NET Mvc application like below: 正如问题所说的那样,但是为了给你提供更多信息,我在我的Asp.NET Mvc应用程序中包含了所有.js
文件,如下所示:
<script src="~/Scripts/jquery-1.10.2.js"></script>
Even that I have to include .js
files in every single page in order to be able to work with jQuery else it doesn't work. 即使我必须在每个页面中包含.js
文件,以便能够使用jQuery,否则它不起作用。 I tried to include these script files in BundleConfig
surprisingly the scripts where not working at all even on the layout page. 我试图将这些脚本文件包含在BundleConfig
令人惊讶的是脚本甚至在布局页面上都没有工作。 What is the trick?! 有什么诀窍?!
In general you have got an App_Start/BundleConfig.cs in your project 通常,您的项目中有一个App_Start / BundleConfig.cs
public class BundleConfig
{
public static readonly string SiteJquery1Bundle = "~/bundles/jquery1";
public static readonly string SiteJquery2Bundle = "~/bundles/jquery2";
public static void RegisterBundles(BundleCollection bundles)
{
// We will choose bundle to render depending on IE version in view
bundles.Add(new Bundle(SiteJquery1Bundle)
.Include("~/Scripts/jquery-1.10.2.js"));
bundles.Add(new Bundle(SiteJquery2Bundle)
.Include("~/Scripts/jquery-2.1.3.min.js"));
}
In your Shared/_Layout.cshtml view add: 在您的Shared / _Layout.cshtml视图中添加:
<html>
<head>
<title>...</title>
<!--[if lt IE 9]>
@Scripts.Render(BundleConfig.SiteJquery1Bundle)
<![endif]-->
<!--[if gte IE 9]><!-->
@Scripts.Render(BundleConfig.SiteJquery2Bundle)
<!--<![endif]-->
...
</head>
So, default ASP.NET MVC projects adds _ViewStart.cshtml which defines single layout in every view. 因此,默认的ASP.NET MVC项目添加了_ViewStart.cshtml,它定义了每个视图中的单个布局。 And check bundle registration in Global.asax.cs 并检查Global.asax.cs中的包注册
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.