最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to inject page specific js into _Layout - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 11

Razor 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>

}
发布评论

评论列表(0)

  1. 暂无评论