a blog for those who code

Friday 17 February 2017

Validate Errors on Compile Time in ASP.Net MVC Razor View

In this post we will be discussing about how to validate errors on compile time in ASP.Net MVC Razor View. As you might know that views does not generate compile time errors, so it very difficult to find out errors when your number of views increases because each time you have to run the application and see if there is any error in views or not.

In ASP.Net MVC models and controllers are pre-compiled whereas views are compiled at run-time, so any error in view cannot be detected until and unless it is run on the browser. By default ASP.Net MVC does not allow you to detect errors at compile time but you can change it by changing the MvcBuildViews property.


After that reload the project and build it on Release mode, you will not get error in Debug Mode. After building if you have any error in your Razor View you will get an error like below, in our case we have misspelled ViewBag to ViweBag.


One more option is to use RazorGenerator. RazorGenerator is a custom tool for Visual Studio that allows processing Razor files at design time instead of run time. So when .cshtml will be compiled using RazorGenerator it will create .generated.cs file thus will give you an error when you will compile it.

Please Like and Share CodingDefined.com blog, if you find it interesting and helpful.

No comments:

Post a Comment