如何在MVC Core中要求/引用config.json

时间:2016-12-12 08:26:41

标签: javascript asp.net-mvc asp.net-mvc-5 gulp asp.net-core-mvc

这行在ASP.NET MVC 5中运行良好,但现在在ASP.NET Core MVC中给我一个错误,我应该如何引用自己的json文件?它只包含我想在gulpfile中使用的一些绝对路径和相对路径。

var gulp = require('gulp'),
    rimraf = require("gulp-rimraf"),
    concat = require("gulp-concat"),
    cssmin = require("gulp-cssmin"),
    uglify = require("gulp-uglify"),
    watch = require('gulp-watch'),
    bs = require("browser-sync").create(),
    less = require("gulp-less"),
    changed = require('gulp-changed'),
    imagemin = require('gulp-imagemin'),
    config = require('./Config.json'), // <--- this line fails (It's line 15)
    urlAdjuster = require('gulp-css-url-adjuster'),
    streamqueue = require('streamqueue');

错误讯息:

Failed to run "...\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
module.js:415
throw err;
^
SyntaxError: D:\...\Config.json: Unexpected token /
at Object.parse (native)
at Object.Module._extensions..json (module.js:412:27)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (D:\...\gulpfile.js:15:14)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)

Config.json

{
  //"compilerOptions": {
  //  "noImplicitAny": false,
  //  "noEmitOnError": true,
  //  "removeComments": false,
  //  "sourceMap": true,
  //  "target": "es5"
  //},
  //"exclude": [
  //  "node_modules",
  //  "wwwroot"
  //]
  "core": {
    "project_name": "Core Web Assets",
    "base_folder": "Core Web Assets",
    "content_folder": "Core Content",
    "scripts_folder": "Core Scripts",
    "source_content": "../Core Web Assets/Core Web Assets/Core Content",
    "source_scripts": "../Core Web Assets/Core Web Assets/Core Scripts",
    "less": {
      "all": "../Core Web Assets/Core Web Assets/Core Content/Less/**/*.less",
      "factory": "../Core Web Assets/Core Web Assets/Core Content/Less/factory/bootstrap.less"
    },
    "css": [
      "../Core Web Assets/Core Web Assets/Core Content/Css/scroller-media-jquery.dataTables.css",
      "../Core Web Assets/Core Web Assets/Core Content/Css/toastr.css",
      "../Core Web Assets/Core Web Assets/Core Content/Css/animate.css",
      "../Core Web Assets/Core Web Assets/Core Content/Css/sweetalert.css"
    ],
    "icon_css": [
      "../Core Web Assets/Core Web Assets/Core Content/Css/font-awesome.min.css",
      "../Core Web Assets/Core Web Assets/Core Content/Css/ionicons.min.css"
    ],
    "scripts": [
      "../Core Web Assets/Core Web Assets/Core Scripts/Utils/**/*.js",
      "../Core Web Assets/Core Web Assets/Core Scripts/Common/*.js"
    ],
    "icons": [
      "../Core Web Assets/Core Web Assets/Core Content/icons/font_awesome/fontawesome-webfont.eot",
      "../Core Web Assets/Core Web Assets/Core Content/icons/font_awesome/fontawesome-webfont.svg",
      "../Core Web Assets/Core Web Assets/Core Content/icons/font_awesome/fontawesome-webfont.ttf",
      "../Core Web Assets/Core Web Assets/Core Content/icons/font_awesome/fontawesome-webfont.woff",
      "../Core Web Assets/Core Web Assets/Core Content/icons/font_awesome/fontawesome-webfont.woff2",
      "../Core Web Assets/Core Web Assets/Core Content/icons/font_awesome/FontAwesome.otf",
      "../Core Web Assets/Core Web Assets/Core Content/icons/ionicons/ionicons.eot",
      "../Core Web Assets/Core Web Assets/Core Content/icons/ionicons/ionicons.svg",
      "../Core Web Assets/Core Web Assets/Core Content/icons/ionicons/ionicons.ttf",
      "../Core Web Assets/Core Web Assets/Core Content/icons/ionicons/ionicons.woff"
    ],
    "images": "../Core Web Assets/Core Web Assets/Core Content/Images/*"
  },
  "xxx2": {
    "base_url": "http://localhost/",

    "project_name": "xxx2",
    "content": "./wwwroot/Autogenerated",
    "css": "./wwwroot/Autogenerated/Css",
    "icons": "./wwwroot/Autogenerated/Icons",
    "images": "./wwwroot/Autogenerated/Images",
    "scripts": "./Scripts/Autogenerated"
  }
  //,
  //"xxx": {
  //  "project_name": "xxx1",
  //  "base_url": "http://localhost/xxx1/",
  //  "port": "3000",
  //  "content": "./Content/Autogenerated",
  //  "css": "./Content/Autogenerated/Css",
  //  "icons": "./Content/Autogenerated/Icons",
  //  "images": "./Content/Autogenerated/Images",
  //  "scripts": "./Scripts/Autogenerated"
  //}
}

1 个答案:

答案 0 :(得分:1)

显然,require()调用加载和解析JSON文件不支持JSON文件中的注释。

删除这些评论或使用支持它们的plugin