20

So I an into an error at runtime with my asp.net mvc3 app, when I checked the code I see that it has a red line on it, the error list says there is no such function defined but it will still compile fine. The error shows up only at runtime? I'm using T4MVC and I was hoping it would help me dodge problems like this!?

Is this by design or am I doing something wrong here?

enter image description here

The code above should NOT compile because there is no such method there!

It fails at runtime :

enter image description here

This kinda of thing happens a lot, not just for un defined methods, but even variables, etc.

gideon
  • 19,329
  • 11
  • 72
  • 113
  • Possible duplicate of [Compile Views in ASP.NET MVC](http://stackoverflow.com/questions/383192/compile-views-in-asp-net-mvc) – Jon Schneider Sep 29 '16 at 16:26

1 Answers1

33

This is by design. The build of of MVC views is disabled by default. You can enable the build of your MVC views in Visual Studio like that:

  • Right click on your project in Visual Studio
  • Unload project
  • Edit project

  • Change the value for MvcBuildViews from false to true

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    ...
    <MvcBuildViews>true</MvcBuildViews>
    ...

  • Reload project

Next time you compile and there are errors in your MVC views, it will not compile. The downside is, the compilation process will take longer.

Update

Here is an answer on SO, explaining how to avoid the error:

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS

Community
  • 1
  • 1
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
  • +1 yay! awesome! I knew there was a unknown feature somewhere, I just tracked down 2 more errors that would have bit me later! – gideon Feb 16 '11 at 09:09
  • 2
    +1, However when adding this it started complaining about this: `It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS` – Filip Ekberg Feb 16 '11 at 09:13
  • @Filip yes **I got that too**... try Cleaning the solution and building again, it may even mean you have errors in your views too like me! – gideon Feb 16 '11 at 09:36
  • @Filip Ekberg @giddy I updated my answer with a link to a SO answer, explaining how to avoid this error. – Martin Buberl Feb 16 '11 at 12:08
  • At first I thought great this is what i need. But then I got a whole lot of errors, from (i think) compiled views that shouldnt even exist anymore. – ckonig Jun 01 '12 at 14:42
  • @Filip Ekberg: If cleaning the solution is not enough, go and manually remove the folders in your projects obj-directory. Worked for me. – cederlof Feb 20 '13 at 14:29