在asp.net mvc的4组织自定义的JavaScript [英] organizing custom javascripts in asp.net mvc 4
问题描述
我是新来的MVC 4,我想知道我应该把我的自定义JavaScript文件。通过自定义,我的意思是只能在特定浏览或使用PartialViews脚本。
I'm new to mvc 4 and I'm wondering where I should put my custom javascript files. By 'custom', I mean scripts that are only used in specific Views or PartialViews.
我使用地区也一样,所以,增加了复杂性。我在想我把脚本脚本文件夹在应用程序的根目录下,然后在自定义子文件夹。
I use Areas too, so that adds up the complexity. I was thinking about placing my scripts in the Script folder at the root of the application and then in a Custom sub-folder.
我怎么会那么引用该脚本在我的意见?我应该使用自定义捆绑?
How would I then reference the scripts in my Views ? Should I use custom Bundles ?
推荐答案
我认为,添加脚本在你的脚本自定义文件夹
文件夹是要走的路。
您可以创建在 AppStart的\\ BundleConfig.cs
文件的新包,如下所示:
i think that adding your scripts to a custom folder in your scripts
folder is the way to go.
you can create a new bundle in the appstart\BundleConfig.cs
file as follows:
bundles.Add(new ScriptBundle("~/bundles/custom").Include(
"~/Scripts/Custom/myCustom.js",
"~/Scripts/Custom/myCustom2.js"));
和比捆绑
添加到您的看法是这样的:
and than add the bundle
to your view like this:
@section scripts{
@Scripts.Render("~/bundles/custom")
}
这将在 @RenderSection渲染(脚本,必需:false)在
文件。 布局
行
this will be rendered at the @RenderSection("scripts", required: false)
line on your layout
file.
或结果
只能拨打一个特定的剧本
您视图
你可以这样做:
OR
To call only one specific script
for your view
you can do:
@section scripts{
<script src="~/Scripts/Custom/myCustom.js"></script>
}
请注意:您可以从解决方案资源管理器
进入部分拖动脚本文件。你不必写的完整路径。结果
修改 - 似乎很重要,所以我复制这从我最后的评论:结果
为了使用缩小
您需要将脚本添加到包桌和添加 BundleTable.EnableOptimizations = TRUE;
在 BundleConfig
文件或设置&LT;编译调试=在你的
文件。网络假
。配置
note: you can drag the script file from the solution explorer
into the section. you don't have to write the entire path.
EDIT - seems important so i copied this from my last comment:
in order to use minification
you need to add your script to the bundle table and either add BundleTable.EnableOptimizations = true;
to the BundleConfig
file or set <compilation debug="false"
in your web.config
file.
这篇关于在asp.net mvc的4组织自定义的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!