Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

razor syntax with errors compiles when it should not compile

Tags:

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.

like image 728
gideon Avatar asked Feb 16 '11 08:02

gideon


People also ask

What is the Razor syntax?

Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content: client content and server code.

What is razor in ASP.NET 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. You can use it anywhere to generate output like HTML.

What is razor view in asp net core?

A view is an HTML template with embedded Razor markup. Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup.

What is razor in asp net?

What is Razor? Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.


1 Answers

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

like image 116
Martin Buberl Avatar answered Sep 18 '22 14:09

Martin Buberl