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):
However, this results in errors:
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?
You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.
Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic.
From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With