razor syntax with errors compiles when it should not compile

razor syntax with errors compiles when it should not compile

 
 

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

 

Compile Views in ASP.NET MVC

回答1

From the readme word doc for RC1 (not indexed by google)

ASP.NET Compiler Post-Build Step

Currently, errors within a view file are not detected until run time. To let you detect these errors at compile time, ASP.NET MVC projects now include an MvcBuildViews property, which is disabled by default. To enable this property, open the project file and set the MvcBuildViews property to true, as shown in the following example:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>

Note Enabling this feature adds some overhead to the build time.

You can update projects that were created with previous releases of MVC to include build-time validation of views by performing the following steps:

  1. Open the project file in a text editor.
  2. Add the following element under the top-most <PropertyGroup> element: <MvcBuildViews>true</MvcBuildViews>
  3. At the end of the project file, uncomment the <Target Name="AfterBuild"> element and modify it to match the following:
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
</Target>

 

3>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\temp\6ca5e0e4\9aa282d8\App_Web_ww2uriuv.4.cs(55): error CS0234: The type or namespace name 'ECardWallViewModel' does not exist in the namespace 'UK.Connect.Model.ECard' (are you missing an assembly reference?)

看开头第一行找到文件,发现这个报错的文件,根本没有include到项目中。可以直接删除

#pragma checksum "C:\repository\Edenred\UK\UK60\Connect_Backend\UKERecognition\UK.Connect.Recognition.Web\Views\ECard\NotificationECardsView.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "7E9AC76728AA1DA2C6A3CC8E6AB64781E8C8D505"

 

Microsoft.Net.Compilers版本过低导致无法识别C#的高级语法

<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />

修改为

<package id="Microsoft.Net.Compilers" version="3.7.0" targetFramework="net472" developmentDependency="true" />

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(197)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-04-10 111. Minimum Depth of Binary Tree
2019-04-10 asp.net tag
2019-04-10 CssClass="Hidden"和Visible="False"
2019-04-10 What is the difference between visibility:hidden and display:none?
2019-04-10 ID vs UniqueID vs ClientID in webform
2019-04-10 GitLab community edition
2018-04-10 设置断点后,查看堆栈信息,判断错误调用的来源是哪里
点击右上角即可分享
微信分享提示