How to get compile-time errors in views using ASP.NET CORE

How can we get compile-time errors in views and view components in Mvc6?

In MVC 5, it was easy to edit the .csproj file. How to do it now?

+5
source share
3 answers

Answer for RC-1

  • Create the following RazorPreCompilation class:
 namespace YourApp.Compiler.Preprocess { public sealed class RazorPreCompilation : RazorPreCompileModule { protected override bool EnablePreCompilation(BeforeCompileContext context) => true; } } 
  1. Puts this class in {root} / Compiler / Preprocess.

enter image description here

  1. Add a call to AddPrecompiledRazorViews in your ConfigureServices method.
 public void ConfigureServices(IServiceCollection services) { // ... other code ... services .AddMvc() .AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly); // ... other code ... } 

As indicated in other answers, when the views are precompiled, you lose the ability to change them while the application is running. In addition, it seems that you sometimes have to rebuild your project (as opposed to just building) for the new code changes to take effect. For example, fixing a typo in a call to the Razor method may still cause a compilation error until you restore the project.

Directive Update

If you want this start to be done using preprocess directives, wrap them around the AddPrecompiledRazorViews call as follows:

 public void ConfigureServices(IServiceCollection services) { // ... other code ... var mvcBuilder = services.AddMvc() #if !DEBUG mvcBuilder.AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly); #endif // ... other code ... } 
+2
source

You must add class

 public class RazorPreCompilation : RazorPreCompileModule { public RazorPreCompilation(IServiceProvider provider) : base(provider) { this.GenerateSymbols = true; } } 

and add AddPrecompiledRazorViews to the mvc setting in your launch class

see this question

0
source

You can use precompiled views by adding the following class to your project. However, this makes it impossible to edit views at run time. So I added the #if !DEBUG pre-processor directive so that views are only pre-compiled in release mode.

 #if !DEBUG namespace MvcBoilerplate.Compiler.Preprocess { using System; using Microsoft.AspNet.Mvc; /// <summary> /// Enable pre-compilation of Razor views, so that errors in your .cshtml files are caught and displayed /// in the Visual Studio errors window at compile time, rather than your sites users receiving a runtime 500 /// internal server error. Pre-compilation may reduce the time it takes to build and launch your project but will /// cause the build time to increase. It will also stop edit and continue working for .cshtml files. /// </summary> public class RazorPreCompilation : RazorPreCompileModule { } } #endif 

Secondly, in Startup.cs you need to use precompiled views that you can do this:

 public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly); } 

Precompiled views give faster application launch performance.

0
source

Source: https://habr.com/ru/post/1235742/


All Articles