Issue migrating MVC3 Application to MVC4: Compiler

2019-02-08 15:55发布

This is a really weird error, I think it may be a razor bug. I'm using VS 2012, MVC4, Framework 4.5.

Following these instructions: http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 I created a new MVC4 project and then I copied all my code (controllers, views, viewmodels) to it from the MVC3 project.

Eveything worked just fine, until I tested one View which has a custom helper and inside it it has one foreach, one switch, three if statements and then I call some other custom helpers in there too.

It's exactly the same code in both projects, in MVC3 it works, but in MVC4 it shows this message:

Compiler Error Message: CS1513: } expected

So I tried adding one curly bracket but it shows the same error, so I keep adding brackets and it won't stop telling me the same thing.

I googled this issue but I just found this question with no answer: http://www.1771.in/asp-net-mvc-4-issues-migrating-mvc3-view-in-vs-2012-rc.html

has anyone experienced this issue?

7条回答
聊天终结者
2楼-- · 2019-02-08 16:34

I ran into this "Expected }" issue as well and the culprit turned out to be an apostrophe in an HTML comment This seems like a bug in Razor.

Here is an example on how to reproduce this issue in the default MVC 4 application with VS 2012. Just add the following a comment with an apostrophe to the @section featured {} in the default.cshtml. Remove the apostrophe from the comment and it works OK.

@section featured {
    <!-- hello world it's not cool --> 
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <h2>@ViewBag.Message</h2>
            </hgroup>
            <p>...</p>
        </div>
    </section>
}
查看更多
男人必须洒脱
3楼-- · 2019-02-08 16:36

I experienced this error but narrowed it down to a missing slash to close a tag. this worked in MVC3:

@helper metatags()
{
  <meta charset="utf-8">
}

but not in MVC4. it requires this:

@helper metatags()
{
  <meta charset="utf-8" />
}
查看更多
贪生不怕死
4楼-- · 2019-02-08 16:43

The Razor parser of MVC4 is different from MVC3. Razor v3 is having advanced parser features and on the other hand strict parsing compare to MVC3.

You may run into syntax error in view while converting MVC3 to MVC4 if you have not used razor syntaxes in correct manner.

Solution of some common razor code mistakes that are not allowed in Razor v2 are :

--> Avoid using server blocks in views unless there is variable declaration section.

Don’t : @{if(check){body}}
Recommended : @if(check){body}

--> Avoid using @ when you are already in server scope.

Don’t : @if(@variable)
Recommended : @if(variable)

Don't : @{int a = @Model.Property }
Recommended : @{int a = Model.Property }
查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-08 16:52

Most helpful thing to do that will solve 6/10 of these for you is in VS2012

File-> Source Control -> Advanced -> Format this Document.

This will solve any un-closed div's, conditional statements even ul's and li's which cause big errors for .net.

查看更多
成全新的幸福
6楼-- · 2019-02-08 16:53

This may be more of a long shot but sometimes if you are using a keyword it will cause that error

List of Keywords VS 2012 http://msdn.microsoft.com/en-us/library/x53a06bb%28v=vs.110%29.aspx

I know two of the new keywords are await and async for 4.5

See the following for an example of what I am talking about http://www.wduffy.co.uk/blog/css-class-property-asp-net-mvc-htmlattributes/

查看更多
来,给爷笑一个
7楼-- · 2019-02-08 16:54

I had exactly the same issue.

In Razor MVC3 i was accessing the vars like this: @vSuggestion but in MVC4 the @ is not necessary.

My example, i had this code in MVC3 working:

@{
    var vSuggestion = ((dynamic)ViewData["suggestion"]);   
}
<!-- more code here --> 
@{ int suggestion = @vSuggestion;
   switch (suggestion)
   {
       case Suggestion.INCORRECT_PASSWORD:
       case Suggestion.USER_ALREADY_IN_DATABASE:  
           <span>Trata de iniciar sesión de nuevo</span><br />
           <span>Recupera tu contraseña @Html.ActionLink("aquí", "Recover", "Account")</span>
       break;
       case Suggestion.EMAIL_DONT_EXISTS:  
           <span>Comprueba que el correo electrónico está bien escrito</span><br />
           <span>Registrate (abajo)</span>
       break;
   }                 
}

In MVC4, Razor wasn't catching the first curly bracket from the switch statement. So i removed the @ from @vSuggestion and razor parsed the code properly.

Hope it helps.

查看更多
登录 后发表回答