Manually upgraded from MVC3 to MVC4 Beta and getting “not valid @” sign errors
-
29-10-2019 - |
Question
I'm using Razor in both
Any page that using the @ sign. Its almost like either the System.Web.WebPages or System.Web.Helpers are not able to determine the Views
Here is the actual error:
"@" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.
Here is my web.config that is in the /Views folder
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
Solution 2
It turns out switching to MVC4 beta was not handling @@@ escapes properly
I have a twitter client where it will render a person's screen name in a view like this:
@@@TwitterUsername
This will render a @ with the escaped @@ and then render the twitter name with @TwitterUsername
This does not work with MVC4. So I had to change to this and it worked fine:
@Html.Raw("@")@TwitterUsername
Not near as pretty, but it works.
Is there something not right with Razor parsing on MVC4 or am I not doing something right implementing it?
The output of this is:
@jordanshane
OTHER TIPS
Refer to MVC 4 ReleaseNote
There is a section describing Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4
I had a similar problem when upgrading from MVC3 to MVC4 when outputting a 'page' that is an Excel dump in Excel XML format. For example, I had the following line in my XML page layout file:
Doesn't work:
<NumberFormat ss:Format="m/d/yyyy;@@"/>
The parser gave me the following error:
Parser Error Message: ""/>" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.
The Html.Raw("@") answer worked for me, and I wasn't readily able to find any reason for this behavior in the links that several other answers provided. In case anyone is looking for a more terse looking solution, here's another possibility:
Does work:
<NumberFormat ss:Format="m/d/yyyy;@('@')"/>