find a location for property in a new city

Saturday, 27 October 2012

Unexpected "string" keyword after "@" character. Once inside code, you do not need to prefix constructs like "string" with "@"

After upgrading an ASP.NET MVC 3 project to MVC 4 I noticed a change in the Razor parser that threw a Parser Error saying: 'Unexpected "string" keyword after "@" character. Once inside code, you do not need to prefix constructs like "string" with "@"'

Firstly this has always working when it was MVC 3 and Razor v1. I may have been getting the syntax wrong all along but if the syntax allowed it was it really wrong?

What I was doing was trying to put some server code in a Razor helper with no surrounding HTML tags, like this example:

@helper Currency1000s(int? value)
{
    if(value == null)
    {
        -
    }
    else
    {
        @string.Format("{0:C0}k", value / 1000.0)
    }
}

Interestingly if I were to replace line 9 with @value all would be fine. Anyway, it is an easy enough fix, you just need to wrap the string.Format in HTML tags or the text tags as I did here:

@helper Currency1000s(int? value)
{
    if(value == null)
    {
        -
    }
    else
    {
        @string.Format("{0:C0}k", value / 1000.0)
    }
}

Follow britishdev on Twitter

9 comments:

  1. Keyword point of view your post gives us some great information, I really like it because first time in my life i am reading this type of information. New Playground tiles Uk for sale visit http://playgroundtilesuk.co.uk/

    ReplyDelete
  2. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics , i will be always checking your blog thanks. Now i need to my blog, you can play, relax and fun with games at here
    friv games 4
    jogos 6
    friv 5 games
    friv 10

    ReplyDelete
  3. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.Marble India

    ReplyDelete
  4. Good post. I learn some thing tougher on distinct blogs everyday. Most commonly it really is stimulating to learn to read content material from other writers and exercise a specific thing there.Luxury Escapes

    ReplyDelete
  5. Not all are true. Everyone has their own way of thinking but I think they have to reconsider. I like to argue for the most accurate results.
    http://fivenightsatfreddysplay.com

    ReplyDelete
  6. That's amazing dude! Now the local best double glazing companies are doing really well by providing highest quality products and adding latest features for more convenience. Don't they?

    ReplyDelete