Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build does not catch errors in the View in asp.net mvc

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

like image 425
terjetyl Avatar asked Dec 07 '08 16:12

terjetyl


People also ask

Which of the following is used for Handling errors at controller level in ASP.NET MVC?

The HandleErrorAttribute is an attribute that can be used to handle exceptions thrown by an action method or a controller. You can use it to display a custom view on a specific exception occurred in an action method or in an entire controller.

How to handle error in ASP net?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.


3 Answers

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.

like image 120
Jason Whitehorn Avatar answered Oct 17 '22 04:10

Jason Whitehorn


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

like image 44
Dan Atkinson Avatar answered Oct 17 '22 05:10

Dan Atkinson


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>
like image 20
user17060 Avatar answered Oct 17 '22 04:10

user17060