views:

343

answers:

5

Why don't we get compile errors on inline code errors in asp.net mvc views f.eks

<h1><%= ViewData.Model.Title.Tostrig() %></h1>

The code above will build just fine. Wrong spelling in webform controls will give you an error so I can't see why this isn't supported in asp.net mvc

EDIT: Luckily there seem to be a fix included in the first RC for asp.net mvc http://weblogs.asp.net/scottgu/archive/2008/12/19/asp-net-mvc-design-gallery-and-upcoming-view-improvements-with-the-asp-net-mvc-release-candidate.aspx

+10  A: 

The ASPX files in both ASP.NET and ASP.NET MVC are not compiled from the Visual Studio IDE. This is often the cause for "false positives" as you have pointed out.

I often add the aspnet_compiler as a post build action to find those errors. You will add some time to your build process, but in exchange for the wait you should be able to spot these errors more easily.

Jason Whitehorn
A: 

Thx. This is pretty annoying if I change the name of a property or function in my model which I use a lot inline in the views. You wouldn't mind share the exact command you put in your postbuild window to enable this?

TT
A: 

You can see

http://odetocode.com/blogs/scott/archive/2006/10/17/8190.aspx

+9  A: 

Following from Jason's answer, this line should work on most projects:

 C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -v / -p "$(ProjectDir)\"

Or for .NET 4 projects:

 C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler -v / -p "$(ProjectDir)\"

Source - Compiling aspx templates using aspnet_compiler

Dan Atkinson
+5  A: 

All you need to do is change the MvcBuildViews property to true in the project file.

e.g.

 <PropertyGroup>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>