admin管理员组文章数量:1325229
I am working with .NET Core 2.0 MVC. If I wanted to have different js files for different pages, how would I inject this js reference from the Login page, for example, into the bottom of the Shared/_Layout page under the other scripts when the Login page is loaded?
I am working with .NET Core 2.0 MVC. If I wanted to have different js files for different pages, how would I inject this js reference from the Login page, for example, into the bottom of the Shared/_Layout page under the other scripts when the Login page is loaded?
Share Improve this question edited Feb 7, 2018 at 19:35 Uwe Keim 40.8k61 gold badges188 silver badges304 bronze badges asked Feb 7, 2018 at 19:26 user6383418user6383418 4151 gold badge5 silver badges20 bronze badges 1- 2 Possible duplicate of Equivalent of "@section" in ASP.NET Core MVC? – Fran Commented Feb 7, 2018 at 19:31
2 Answers
Reset to default 11Razor views/layouts can be nested and sections rendered into placeholders.
So in the main top level layout you could have
@RenderSection("Scripts", false)
and on the pages have (with deployment options):
@section Scripts {
<environment names="Development">
<script type="text/javascript" src="@Url.Content("~/js/Home/yourpagescript.js")" asp-append-version="true"></script>
</environment>
<environment names="Staging,Production">
<script type="text/javascript" src="@Url.Content("~/js/Home/yourpagescript.min.js")" asp-append-version="true"></script>
</environment>
}
In Shared/_Layout :
@RenderSection("scripts", false) //where you need your script
In a page:
@section scripts {
<script type="text/javascript" src=""></script>
}
本文标签: javascriptHow to inject page specific js into LayoutStack Overflow
版权声明:本文标题:javascript - How to inject page specific js into _Layout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742170627a2426649.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论