BundleConfig设置好,然后在页面就能引用
public class BundleConfig
{
// 有关捆绑的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
// 使用要用于开发和学习的 Modernizr 的开发版本。然后,当你做好
// 生产准备就绪,请使用 https://modernizr.com 上的生成工具仅选择所需的测试。
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/bootstrap.css", "~/Content/site.css"));
}
}
接下来开始引用:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("应用程序名称", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("主页", "Index", "Home", new { area = "" }, null)</li>
<li>@Html.ActionLink("API", "Index", "Help", new { area = "" }, null)</li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - 我的 ASP.NET 应用程序</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
如果布局页面有定义节点 @RenderSection("ScriptSection", required: false) ,那么在子页面中可以使用 @section ScriptSection{} 来引用js或者css。这个作用相当于在布局页中定义了一个占位,在子页面中去填充这个占位
显示结果
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width" /> | |
<title>Home Page_by_xiaozhao_20190317</title> | |
<link href="/Content/bootstrap.css" rel="stylesheet"/> | |
<link href="/Content/site.css" rel="stylesheet"/> | |
<script src="/Scripts/modernizr-2.8.3.js"></script> | |
</head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<body> | |
<div class="navbar navbar-inverse navbar-fixed-top"> | |
<div class="container"> | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a class="navbar-brand" href="/">应用程序名称</a> | |
</div> | |
<div class="navbar-collapse collapse"> | |
<ul class="nav navbar-nav"> | |
<li><a href="/">主页</a></li> | |
<li><a href="/Help">API</a></li> | |
</ul> | |
</div> | |
</div> | |
</div> | |
<div class="container body-content"> | |
<div class="jumbotron"> | |
<h1>ASP.NET</h1> | |
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p> | |
<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p> | |
</div> | |
<div class="row"> | |
<div class="col-md-4"> | |
<h2>Getting started</h2> | |
<p>ASP.NET Web API is a framework that makes it easy to build HTTP services that reach | |
a broad range of clients, including browsers and mobile devices. ASP.NET Web API | |
is an ideal platform for building RESTful applications on the .NET Framework.</p> | |
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301870">Learn more »</a></p> | |
</div> | |
<div class="col-md-4"> | |
<h2>Get more libraries</h2> | |
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p> | |
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301871">Learn more »</a></p> | |
</div> | |
<div class="col-md-4"> | |
<h2>Web Hosting</h2> | |
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p> | |
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301872">Learn more »</a></p> | |
</div> | |
</div> | |
<hr /> | |
<footer> | |
<p>© 2019 - 我的 ASP.NET 应用程序</p> | |
</footer> | |
</div> | |
<script src="/Scripts/jquery-3.3.1.js"></script> | |
<script src="/Scripts/bootstrap.js"></script> | |
</body> | |
</html> | |
原文地址:https://www.cnblogs.com/zhaoxiansheng/p/10545726.html