10

是否可以在 grunt 中监视文件并自动重新加载 ASP.net MVC Web 应用程序。还是 livereload 仅适用于通过 grunt 提供的文件。我遇到了 grunt 插件“grunt-iisexpress”,但不确定我是否可以使用它,以及在文件更改时重新加载 ASP.net MVC webapp 的任务。

我的网络应用程序中没有任何 index.html 作为起始页面,但 _ViewStart.cshtml 启动了整个应用程序。

4

2 回答 2

9

有可能的。我刚刚使用grunt-contrib-watch ( https://github.com/gruntjs/grunt-contrib-watch ) 在我的 ASP.NET 应用程序中进行了实时重新加载。只花了几分钟。

我以这篇文章为指导: http ://www.aliirz.com/javascript/2013/12/25/Live-Reload-with-Grunt/ 。

通过 ASP.NET 应用程序文件夹中的命令提示符执行此操作。

1. 安装 grunt-contrib-watch

如果您还没有 package.json 文件并希望将依赖项保存在一个文件中:

npm init

然后将 Grunt 和 grunt-contrib-watch 添加到您的项目中:

npm install --save-dev  grunt grunt-contrib-watch

2.配置咕噜

Gruntfile.js接下来在同一个文件夹中创建一个。这是我的:

  'use strict';
  module.exports = function (grunt) {
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.initConfig({
      watch: {
        views: {
          files: [
              'Views/**/*.cshtml', 
              'Scripts/**/*.js',
              'Content/**/*.css',
              'Content/images/**/*',            
              'bin/**/*.dll'
          ],
          options: {
            livereload: true,
          }
        }
      }
    });
  }

3.运行实时重载服务器

在 ASP.NET 应用程序旁边运行实时重载服务器:

grunt watch

4. 将片段添加到 ASP.NET

最后,要在您的 ASP.NET 应用程序中启用它,只需将 live-reload 片段添加到您的布局和/或视图中:

<script src="http://localhost:35729/livereload.js"></script>
于 2014-06-09T16:16:54.903 回答
3

我遇到了这个 mvc 生成器:https ://github.com/has606/generator-aspnetmvc也许您可以在项目中执行类似 grunt 文件的操作:

livereload: {
    options: {livereload: 32684},
    files: [
      '<%%= yeoman.app %>/Content/**/*.css',
      '<%%= yeoman.app %>/Scripts/**/*',
      '<%%= yeoman.app %>/Content/images/**/*',
      '<%%= yeoman.app %>/Views/**/*.cshtml',
      '<%%= yeoman.app %>/bin/**/*.dll'
    ]
  }

因此,对视图或编译的任何更改都会重新加载站点

于 2014-02-20T16:48:09.357 回答