Compile your ASP.NET Core MVC Views
December 28, 2016 by Anuraj
aspnet core mvc razor
This post is about compiling your ASP.NET Core MVC Views. Normally in ASP.NET MVC, the views are not compiled until they are requested by the browser. To avoid this you can precompile the views. Precompile also helps to identify any errors upfront than at runtime.
In previous versions in ASP.NET MVC, you can do this by adding / setting <MvcBuildViews>true</MvcBuildViews>
in your CSProj file.
In ASP.NET Core 1.1, Microsoft introduced Precompile tools. You can do this by adding ViewCompilation.Tools
in the tools section and you can either run it manually or can be as part of postpublish
script.
Here is the project.json file. (Included required references only)
And here is the postpublish script section.
Now you can run dotnet publish
command, which will compile the code and views, once it is successfull, you can remove the Views folder or you can use the exclude the option. In previous versions of ASP.NET, the views should not be removed, it was place holders, but in ASP.NET Core, Views are compiled to a DLL.
You can test the application, using dotnet <dllname>
, which will host the application in Kestrel HttpServer.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub