开发者

In a view, why does visual studio not know that a semi colon after a model directive is going to cause a runtime error?

I'm very very new to MVC3, and ran in to a problem very soon after going thru the asp.net mvc3 tutorials from pluralsight.

Trying out the strongly typed example of the introduction 'adding a model' section, This works:

@model MvcApplication1.Models.RestaurantReview
@{
    ViewBag.Title = "Home Page 1";
}
... 

But this doesn't... it builds, and then chokes on a runtime error:

@model MvcApplication1.Models.RestaurantReview;
@{
    ViewBag.Title = "Home Page 1";
}
... 

The only difference being that semi colon after the @model directive, turning it in to a statement, rather than a directive.

Why doesn't visual studio know that this is going to crash? The error message that IIS presents during the crash isn't even the correct line number of where the error actually is in the code.

VS do开发者_如何转开发es underline the line in red, telling me that only such and such can be used in a statement, but it doesn't prevent the build success.

This seems like it would be a nightmare to debug in a big project if it wasn't caught right away, no?


It's obviously a bug. We are aware of it and will fix it for the next version of MVC (in fact the fix is already in the MVC 4 Developer Preview).


Compile your views on the build event and it should make your views choked if there is any syntax error.

To make your views to be compiled, please do the following;

  1. Unload your project by right right clicking the project on the solution explorer in VS and clicking unload project
  2. Right click the project which has been converted to unavailable project and click "Edit your_project_name.csproj" (that would be vbproj I guess if your project is VB project)
  3. See the following code;

    <!--There some lines of code here and I deleted them to get to the point quickly-->
    
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <MvcBuildViews>false</MvcBuildViews>
    

  4. Change the MvcBuildViews node value from false to true

  5. After this action, save it and reload your project.

Then, build your solution to compile it. You will see that your views will be compiled as well.

UPDATE

Tried your example and it choked as supposed. So it has Works on My Machine Seal of Approval

In a view, why does visual studio not know that a semi colon after a model directive is going to cause a runtime error?

UPDATE

I have written a quick blog post on this topic. See the details :

http://www.tugberkugurlu.com/archive/how-to-detect-errors-of-our-asp-net-mvc-views-on-compile-time-blow-up-in-my-face-theory

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜