The best way to combat shaving

We are in the process of updating the .NET mvc application from MVC3 to MVC5, which includes an upgrade from Razor 1 to Razor 3. One error that we continue to see in the views is the following:

In MVC3, many of our views do this:

@if (someCondition)
{
    @* render some content *@
    @string.Format(...)
}

However, in MVC5 this gives the following error:

System.Web.HttpParseException: Unexpected keyword "string" after "@" character. After inside the code, you do not need prefix constructs such as "string" with "@".

There are two simple fixes that I know of:

@if (someCondition)
{
    @* render some content *@
    @: @string.Format(...)
}

or

@if (someCondition)
{
    @* render some content *@
    <text> @string.Format(...) </text>
}

However, it will be painful to make and verify these changes in hundreds of submissions. So my question is: what is the best way to eliminate this seemingly violation?

  • - , , , ?
  • Razor 3?
+4
2

, . , , , , @string @String ( , , ), , , .

@if (condition)
{
    @String.Format("test {0}", 2992)
}

, , / "@string" "@String" .

+2

, ,

@if (someCondition)
{
    string.Format(...)
}

,

@if (someCondition)
{
    @* render some content *@
    @: @string.Format(...)
}

Or

@if (someCondition)
{
    @* render some content *@
    <text> @string.Format(...) </text>
}
+2

Source: https://habr.com/ru/post/1535559/


All Articles