在asp.net mvc的4组织自定义的JavaScript [英] organizing custom javascripts in asp.net mvc 4

查看:80
本文介绍了在asp.net mvc的4组织自定义的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆