ASP.NET Core 2.2 MVC开发与产品环境中的捆绑 [英] Bundling in ASP.NET Core 2.2 MVC dev vs prod environments

查看:48
本文介绍了ASP.NET Core 2.2 MVC开发与产品环境中的捆绑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序设置为不在开发中时捆绑css和js文件,而在开发时不捆绑.

I am setting up my application to bundle css and js files when not in development, and not bundle when in development.

为此,我首先有一个bundleconfig.json文件:

To do that I first have a bundleconfig.json file:

[
  {
    "outputFileName": "wwwroot/css/bundle.min.css",
    "inputFiles": [
      "wwwroot/lib/bootstrap/bootstrap.min.css",
      "wwwroot/lib/jqueryui.jquery-ui.min.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/bundle.min.js",
    "inputFiles": [
      "wwwroot/lib/jquery/jquery.min.js",
      "wwwroot/lib/jqueryui/jquery-ui.min.js",
      "wwwroot/lib/bootstrap/dist/js/bootstrap.bundle.js"
    ]
  }
]

然后在我的页面中,我有一个标题标签:

Then in my page I have a head tag:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewBag.Title</title>
    <environment exclude="development">
        <link rel="stylesheet" href="~/css/bundle.min.css" asp-append-version="true" />
        <script type="text/javascript" src="~/js/bundle.min.js" asp-append-version="true"></script>
    </environment>

    <environment include="development">
        <link rel="stylesheet" href="~/lib/bootstrap/bootstrap.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/lib/jqueryui/jquery-ui.css" asp-append-version="true" />

        <script type="text/javascript" src="~/lib/jquery/jquery.js" asp-append-version="true"></script>
        <script type="text/javascript" src="~/lib/jqueryui/jquery-ui.js" asp-append-version="true"></script>
        <script type="text/javascript" src="~/lib/bootstrap/dist/js/bootstrap.bundle.js" asp-append-version="true"></script>
    </environment>
</head>

一切正常.我只是不喜欢必须复制budingconfig.json和标头中开发环境标签中的文件列表这一事实.

This all works fine. I'm just not a fan of the fact that I have to duplicate the list of files in the budingconfig.json and in the development environment tag in the header.

在WebForms项目中,我可以使用<%:Scripts.Render("...")%>,如果处于开发模式,它将为捆绑包中的每个项目生成链接,并且将为捆绑包生成1个链接如果不是在开发模式下..net核心MVC项目中是否也有类似的东西?

In WebForms project I can use <%: Scripts.Render("...") %> and it will generate links for each item in the bundle if in development mode, and it will generate 1 link for the bundle if not in development mode. Is something like this available in .net core MVC projects as well?

推荐答案

ASP.NET Core中没有内置方法可以做到这一点.但是,直接滚动自己的内容是非常直接的.

There is no build-in way to do this in ASP.NET Core. However it is pretty straight forward to roll your own.

Mad Christensen为MVC5构建了一个解包程序,并且此处是适应它的要点.NET Core.

Mad Christensen has build an unpacker for MVC5 and here is a gist that adapts it to .NET Core.

您可以这样使用它:

<environment names="Development">
    @Bundler.Unpack(HostingEnvironment.ContentRootPath, "/js/site.min.js")
</environment> 

但是,如果除调试之外没有其他特定的理由要包含每个文件,则还可以依赖Sourcemap.bundleconfig中为此有一个标志.("sourceMap":true )

However, if you have no specific reason to include each file other than debugging you can also rely an sourcemaps. There is a flag in the bundleconfig for this. ( "sourceMap": true )

这篇关于ASP.NET Core 2.2 MVC开发与产品环境中的捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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