网络核心 2:将 BundleConfig.cs 转换为 BundleConfig.json

Net Core 2: Convert BundleConfig.cs to BundleConfig.json

如何将下面的BundleConfig.cs转换成BundleConfig.json?我正在将 .NET 4.6.2 项目迁移到 NET ASP Core 2。收到以下错误,并尝试进行研究。还试图阅读微软文档 https://docs.microsoft.com/en-us/aspnet/core/client-side/bundling-and-minification?view=aspnetcore-2.2&tabs=visual-studio

原版BundleConfig.cs

namespace Test.Web
{
    /// <summary>
    /// Bundle configs registration common class.
    /// </summary>
    public class BundleConfig
    {
        /// <summary>
        /// Register Bundles.
        /// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
        /// </summary>
        /// <param name="bundles"></param>
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/misc/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/misc/bootstrap.js",
                      "~/Scripts/misc/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }
    }
}

尝试的解决方案: 从第三段开始

[
  {
    "outputFileName": "wwwroot/css/site.min.css",
    // An array of relative input file paths. Globbing patterns supported
    "inputFiles": [
      "wwwroot/css/site.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optionally generate .map file
    "sourceMap": false
  },
  {
    "outputFileName": "~/bundles/jquery",
    "inputFiles": [
      "\"~/Scripts/jquery/jquery-{version}.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optionally generate .map file
    "sourceMap": false
  },

收到错误:

Value must match regular expression \.css$
Value must match regular expression \.js$

您的 "outputFileName" 应该以 .js 或 .css.

结尾

只是改变

"outputFileName": "~/bundles/jquery",

"outputFileName": "wwwroot/Scripts/bundles/jquery.js",

它不会显示该错误。 同样在 .net 核心中,所有静态文件都从 wwwroot 文件夹提供,因此您应该将“~/bundles/”更改为 "wwwroot/bundles/BundleName.js" 或 "wwwroot/Scripts/bundles/BundlesName.js".