我已经在我的MVC项目中加载了一些外部JavaScript和一些内联JavaScript到View页面。我听说过响应过滤器在内容加载后将这些脚本移动到页面末尾,但我对此并不完全了解。
我如何在整个项目中实现这一目标?
答案 0 :(得分:3)
本文http://weblogs.asp.net/imranbaloch/bundling-and-minifying-inline-css-and-js介绍了解决问题的方法。
我希望它有所帮助。
答案 1 :(得分:0)
我会使用构建任务(使用Grunt或Gulp)将脚本组合和缩小为静态内容,并重写HTML以在页面中包含CSS和JS资源。
您可以使用Gulp任务(例如https://www.npmjs.com/package/gulp-uglify)进行组合和缩小,然后https://github.com/jonkemp/gulp-useref
整个任务看起来像这样:
s
除了缩小和组合资源之外,它还允许您定义要为输出定位的HTML部分:
gulp.task('html', function () {
var assets = useref.assets();
return gulp.src('app/*.html')
.pipe(assets)
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest('dist'));
});
并将其重写为:
<html>
<head>
<!-- build:css css/combined.css -->
<link href="css/one.css" rel="stylesheet">
<link href="css/two.css" rel="stylesheet">
<!-- endbuild -->
</head>
<body>
<!-- build:js scripts/combined.js -->
<script type="text/javascript" src="scripts/one.js"></script>
<script type="text/javascript" src="scripts/two.js"></script>
<!-- endbuild -->
</body>
</html>