Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for razor errors during build

Is there a way for Visual Studio (I'm using 2010) to find errors within razor views during builds, in the same way as other code in a C# project would?

It's just a pain that you can check any errors in your code and think that everything's fine, but it appears that you can't be sure about views unless you go through each one.

BTW I obviously don't code in my views - I'm just talking about HTML or URL extension methods for example.

like image 592
isNaN1247 Avatar asked Apr 14 '11 17:04

isNaN1247


People also ask

Are Cshtml files compiled?

cshtml extension are compiled at both build and publish time using the Razor SDK. Runtime compilation may be optionally enabled by configuring your project.

Is Cshtml a razor?

cshtml files are razorpages or MVC views, they does not contain any C#-written client-side code. If you wan to do so, you must use JavaScript. However, a . razor file, also know as a Razor component, can have C# written in it and run on client's browser.

What is @model in razor?

New @model directive Let's now look at a new feature we added with the ASP.NET MVC 3 Beta – the @model directive. The @model directive provides a cleaner and more concise way to reference strongly-typed models from view files.

What is Razor syntax in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.


2 Answers

Try setting MVCBuildViews to true in your project file (i.e. edit your csproj file)

 <MvcBuildViews>true</MvcBuildViews> 
like image 140
JP. Avatar answered Oct 13 '22 22:10

JP.


Building views takes a while and the extra 10+ seconds to do a debug build can get annoying fast, so I usually only set the MvcBuildViews to true on release type build configurations. That way, if you have a build server it will catch the error for you, or you can manually run a release build every now and then to check your views.

I don't think order matters for PropertyGroup elements, but for a more complete example i included elements above and below the MvcBuildViews element.

<PropertyGroup>     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>     ...     <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>     <MvcBuildViews>false</MvcBuildViews>     <UseIISExpress>false</UseIISExpress>     ... </PropertyGroup>  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">     ...     <ErrorReport>prompt</ErrorReport>     <MvcBuildViews>true</MvcBuildViews>     <WarningLevel>4</WarningLevel>     ... </PropertyGroup> 

The MvcBuildViews element in the top PropertyGroup was added by VS on project creation, the build configuration specific one (bottom PropertyGroup) i added manually

like image 33
Tikall Avatar answered Oct 13 '22 21:10

Tikall