.net core3.1 mvc视图运行时编译


有时为了方便调试,当修改cshtml文件时,不需要重新编译,操作方法如下

  • 如果是新项目,在创建项目时“启用Razor运行时编译”打勾即可  
  • 如果是现有项目

  方法1(原理同上):

  1.项目右键->编辑项目文件:

1 "Microsoft.NET.Sdk.Web">
2   
3     netcoreapp3.1
4     false
5   
6   
7     
8   
9 

  2.launchSettings.json

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
      }
    },
    "WebApplication4": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
      }
    }
  }

 方法2:

1.编辑项目文件如下


  
    netcoreapp3.1
  
  
    
  

2.修改startp.cs文件  

    public class Startup
    { 
        public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
            Env = env;
        }
        public IWebHostEnvironment Env { get; set; }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IMvcBuilder mvcBuilder = services.AddControllersWithViews();
#if DEBUG
            if (Env.IsDevelopment())
                mvcBuilder.AddRazorRuntimeCompilation(); 
#endif
        }

最后:以上无论哪种方法都加上这个条件后,会使用发布后的文件夹比较干净

Condition="'$(Configuration)' == 'Debug'" />

未添加 Condition="'$(Configuration)' == 'Debug'"

添加 Condition="'$(Configuration)' == 'Debug'"后: