I have the following code inside a partial view:
@if (Request.IsAuthenticated)
{
Hello, @Html.ActionLink(User.Identity.Name, "ChangePassword", "Account") | Html.ActionLink("Log off", "LogOff", "Account")
}
else
{
@Html.ActionLink("Log in", "Login", "Account")
}
Expected output (with appropriate links to actions):
- If logged in: Hello Jim | Log off
- If not logged in: Log in
However, this results in errors:
- Within VS, The word “Hello” has an error on it: “Cannot resolve symbol ‘Hello'” and “&#” has “Expression expected”
- In the browser I get “CS1040: Preprocessor directives must appear as the first non-whitespace character on a line”
If I put a <p>...</p> around the line beginning with “Hello” the error goes away.
There is obviously some syntax error with my mixing calls to @Html and text within the same line. What is the correct way to do this?
The contents of a code block (
{ ... }) are expected to be code, not markup.If you want to put text directly in a code block, you have three choices:
<text>tag, which will just render the text without the tag@:, which is equivalentSee SottGu’s blog post.