How to put an @ inside {} in MVC
When making a page in MVC5 with Razor, I put the following code
{--conteúdo da @ RenderBody() etc, etc }
Generated an error because @ is inside keys
Error:
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: A space or line break was encountered after the "@" character. Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following "@" with no space in between.
Source Error:
Line 74: </aside>
Line 75: <!-- PAINEL PRINCIPAL -->
Line 76: {--conteúdo da @ RenderBody()
Line 77: <div id="main">
Line 78: <div id="ribbon">
Source File: /Views/Home/padroes_layout.cshtml Line: 76
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009
How to place the arroba inside? and if I want to write @RenderBody() has no how?
2 answers
Alternative 1
Just type the duplicate Symbol: @@
.
This will be turning into just one @
in the output.
In short, correlating with your question, to write "@RenderBody()"
to the output of a razor view, just write like this:
@@RenderBody()
Alternative 2
Another option, which may be useful in other cases, is to have razor render an object, which can be anything, including the string "@RenderBody()"
:
@("@RenderBody()")
Using ( @ ) in Razor
"the special character @ starts an expression, a block with a single statement or several, for example, we can insert a Razor code directly into the HTML markup, using the character @ to start a condition expression or present the contents of a variable."(Alexandre Tadashi Sato, introduction to ASP.NET Razor. Available http://msdn.microsoft.com/pt-br/library/gg675215.aspx>, accessed April 30 2014)
How to use:
declaring and using variables
@{
var texto = "Mostrar um Texto";
var ativo = false;
}
<p>@texto</p>
with if / else
@if (ativo){
<div>Está ativo</div>
} else {
<div>Não está ativo</div>
}
Com for
@for(var a = 0; a <= 5; a++)
{
<p>Imprimir número: @a</p>
}
With While
@{
var cont = 0;
}
@while (cont < 100){
<p>@cont</p>
cont++;
}
On the question
The RenderBody means that the page will be rendered here, it already has a functionality and I do not see the need to print the text in this way, since razor demonstrates other forms as mentioned above.
works:
@RenderBody();
@{
@RenderBody();
}
not working
@{ @@RenderBody(); }
works as an on-screen text discaraterizing such functionality
{@@RenderBody();}
references