剃刀语法错误编译时,它不应该编译 [英] razor syntax with errors compiles when it should not compile

查看:241
本文介绍了剃刀语法错误编译时,它不应该编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在运行时与我的asp.net mvc3应用程序错误,当我检查代码,我看到它有一个红线,错误列表说是没有这样的函数定义,但它仍然会编译良好。错误仅在运行时显示?我正在使用 T4MVC ,我希望它能帮助我避免这样的问题?



这是设计还是我做某事



p>

上面的代码不应该编译,因为没有这样的方法!



它在运行时失败:





这种事情发生了很多,不仅仅是未定义的方法,甚至变量等。

解决方案

这是设计。默认情况下禁用MVC视图的构建。您可以在Visual Studio中启用您的MVC视图的构建:




  • 在Visual Studio中右键单击您的项目

  • 卸载项目

  • 编辑项目

  • MvcBuildViews 的值从false更改为 true

    <?xml version =1.0encoding =utf-8?>

    < Project ToolsVersion =4.0 DefaultTargets =Buildxmlns =http://schemas.microsoft.com/developer/msbuild/2003>

    ...

    < MvcBuildViews> true< / MvcBuildViews>


  • ul>

    下次编译,并且在您的MVC视图中有错误,它将无法编译。
    缺点是编译过程需要更长时间。



    更新



    以下是回答 on SO,解释如何避免错误:


    使用注册为allowDefinition ='MachineToApplication'水平。此错误可能是由于虚拟目录未在IIS中配置为应用程序引起的



    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?

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

    It fails at runtime :

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

    解决方案

    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

    这篇关于剃刀语法错误编译时,它不应该编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆