Resolving the “Server Error in ‘/’ Application” Error in ASP.Net

I’ve been doing a lot more MVC in ASP.net lately and I’m running into all sorts of new challenges as I learn things like Razor scripting.

Server Error in '/' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: "-" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.
Source Error:
Line 498: msViewportStyle.appendChild(
Line 499: document.createTextNode(
Line 500: '@-ms-viewport{width:auto!important}'
Line 501: )
Line 502: );

Well that’s not good, at the end of my body was a script block for bootstrap that was causing some issues. It was injecting some CSS using javascript and the @ symbol was being interpreted as an opening code block and then would error out because what it found after that was not valid code. This made sense, but was the solution? My first instinct was to escape it somehow, so I played with putting a backslash and then a double backslash in front of it, but that didn’t work. So I then went and researched what the escape character was, and found that there wasn’t a clear answer to this.

I found many posts on Stack Overflow and various other blocks and forums. Here’s a list of some of the suggestions I found:

@@
@:
@(“@”)
@Html.Raw(‘@’)

Those were just the more succinct options. Many people suggestion wrapping the whole script block inside of a razor code block and spitting it back out as just text. I really wasn’t sure which was the best option so I tried the @ option and found it did work for me, but if for some reason it doesn’t work for you try one of the others! Here’s what my code looked like in the end:

Line 498: msViewportStyle.appendChild(
Line 499: document.createTextNode(
Line 500: '@@-ms-viewport{width:auto!important}'
Line 501: )
Line 502: );