Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue migrating MVC3 Application to MVC4: Compiler Error Message: CS1513: } expected

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?

like image 743
marcos.borunda Avatar asked Oct 09 '12 23:10

marcos.borunda


4 Answers

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 }
like image 166
Dhrumil Bhankhar Avatar answered Nov 20 '22 15:11

Dhrumil Bhankhar


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.

like image 42
Vic Avatar answered Nov 20 '22 15:11

Vic


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>
}
like image 4
Hector Correa Avatar answered Nov 20 '22 16:11

Hector Correa


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/

like image 1
mosesfetters Avatar answered Nov 20 '22 16:11

mosesfetters