Implementing a switch statement in a CSHTML page
@switch (id)
{
case "test": <h1>Test Site</h1>
break;
case "prod": <h1>Prod Site</h1>
break;
}
There is no need to enclose the entire switch statement in a @{}
block, (unlike Joel Etherton's post)
Your errors are basically regular syntax errors and have nothing to do with razor;
the variable wasn't in parenthesis
the body of switch wasn't in brackets
no "break" after the first statement.
Your switch needs to be completely enclosed in a block and it needs to be "broken" properly:
// Use the @{ } block and put all of your code in it
@{
switch(id)
{
case "test":
// Use the text block below to separate html elements from code
<text>
<h1>Test Site</h1>
</text>
break; // Always break each case
case "prod":
<text>
<h1>Prod Site</h1>
</text>
break;
default:
<text>
<h1>WTF Site</h1>
</text>
break;
}
}
Because the <h1>
tags are enclosed html blocks by themselves, you may not need the <text>
blocks for separation. It's just my habit to include them.