在没有mvc的angularjs spa中捆绑和缩小

时间:2016-05-30 06:55:58

标签: asp.net angularjs asp.net-mvc asp.net-mvc-4 bundling-and-minification

我正在创建一个与REST API通信的AngularJS站点。对于REST API,我使用的是ASP.NET Web API。我还创建了一个“ASP.NET空Web应用程序”。此项目中只有HTML,js和CSS文件(以及web.config)。我想将我的js和CSS文件捆绑和缩小,但我不想将缩小的文件添加到index.cshtml中。有可能吗?

任何人都可以说如何将缩小的文件添加到index.html。

我跟随的步骤。

1.安装包装。
 Install-Package Microsoft.AspNet.Web.Optimization
2.处理BundleConfig类并捆绑:

 using System.Web.Optimization;
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle(new ScriptBundle("~/MyApp/Main")
                .Include("~/MyApp/app.js")
                .Include("~/MyApp/SampleController.js")); 
        } 
    }

3.在global.asax

中的应用程序启动中注册BundleConfig类
void Application_Start(object sender, EventArgs e)
{
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

现在我想将Main.js引用到我的application.can我可以将main.js添加到我的index.html中,如果是的话怎么样?
是否必须创建.cshtml然后使用

引用main.js
@script.Render

还有其他方法可以捆绑和缩小我的文件而不是asp.net mvc ??

1 个答案:

答案 0 :(得分:0)

首先,您需要针对此问题优化您的角色代码:Angularjs minify best practice

之后,您需要将所有Angular文件收集到一个包中。

public static void RegisterBundles(BundleCollection bundles) {
    bundles.Add(new ScriptBundle("~/angular").Include(
        "~/Scripts/first-file.js",
        "~/Scripts/second-file.js",
        "~/Scripts/third-file.js"));

    BundleTable.EnableOptimizations = true;
}

您可以使用以下标记访问* .cshtml中的捆绑JavaScript文件:

@Scripts.Render("~/angular")

Here是关于这个问题的好博客文章。