是否有将自定义 Javascript 添加到 ASP.NET MVC 5 页面的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25720250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:53:53  来源:igfitidea点击:

Is there a correct way to add custom Javascript to an ASP.NET MVC 5 page?

javascriptjqueryasp.netasp.net-mvcrazor

提问by yesman

At the moment, I have added the jQuery source file to my ASP.NET project's Scripts folder. In the _Layout.cshtml page, I have included ~/Scripts/jquery-2.1.1.min.js. Right now, I can include jQuery code on every page I make this way:

目前,我已将 jQuery 源文件添加到我的 ASP.NET 项目的 Scripts 文件夹中。在 _Layout.cshtml 页面中,我包含了 ~/Scripts/jquery-2.1.1.min.js。现在,我可以在我以这种方式制作的每个页面上包含 jQuery 代码:

If this page shows a popup I was succesfull.

<script>
$(document).ready(function(){
alert("works!");
});
</script>

However, I don't want to include a full script in every view. I would prefer to create a seperate JS file, put it in my Scripts folder, and then include it using Razor.

但是,我不想在每个视图中都包含完整的脚本。我更喜欢创建一个单独的 JS 文件,将它放在我的 Scripts 文件夹中,然后使用 Razor 包含它。

@{ 
    //Razor Magic inserting Javascript method!
 }

If this page shows a popup I was succesfull.

How do I do this? And is it the "correct" way to include a unique Javascript file for a single page?

我该怎么做呢?它是为单个页面包含唯一 Javascript 文件的“正确”方式吗?

采纳答案by Kartikeya Khosla

Best and elegent way to solve your problem in MVC is making BundlesKnown as Bundling and Minification.

贝斯特和高贵的方式来解决MVC您的问题正在使Bundles素有Bundling and Minification

See more here :- http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

在此处查看更多信息:- http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

回答by Swati Gupta

You can put your page specific JS code in a separate JS file and can refer to your specific Views using the following piece of Code:

您可以将页面特定的 JS 代码放在单独的 JS 文件中,并可以使用以下代码段引用您的特定视图:

1) You need to put this section in your _Layout.cshtml

1) 您需要将此部分放在您的_Layout.cshtml 中

<head>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-2.1.1.min.js)">
    @RenderSection("JavaScript", required: false)
</head>

2) You need to add @section to the View in which you want to refer your JS file - (_ABCDView.cshtml)

2) 您需要将@section 添加到要在其中引用 JS 文件的视图 - (_ABCDView.cshtml)

@section JavaScript
{
   <script type="text/javascript" src="@Url.Content("/Scripts/SampleScript2.js")"></script>

}

NOTE:@RenderSection, false, means that the section is not required on a view that uses this master page, and the view engine will ignore the fact that there is no "JavaScript" section defined in your view. If true, the view won't render and an error will be thrown unless the "JavaScript" section has been defined.

注意:@RenderSection,false,意味着在使用此母版页的视图上不需要该部分,并且视图引擎将忽略在您的视图中没有定义“JavaScript”部分的事实。如果为 true,除非定义了“JavaScript”部分,否则视图将不会呈现并抛出错误。

And you are good to go!

你很高兴去!

Hope this will help you.

希望这会帮助你。

Thanks, Swati

谢谢,斯瓦蒂