Razor allows arguments to ASP.NET Core MVC tag helpers to be written as inline C# expressions within the corresponding attribute declaration. However, since HTML attributes are delimited by quotation marks, what's the syntax if such an expression should itself contain a quotation mark?
Here's a sample from https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring:
<website-information info="new WebsiteContext {
Version = new Version(1, 3),
CopyrightYear = 1638,
Approved = true,
TagsToShow = 131 }" />
What would this look like if one of the WebsiteContext
properties were to take a string literal?
The syntax to register all the tag helpers that are in an assembly is to use asterisk comma (*,) and then the assembly name, Microsoft. AspNet. Mvc. TagHelpers.
HtmlHelpers vs. Unlike HtmlHelpers, a tag helper is a class that attaches itself to an HTML-compliant element in a View or Razor Page. The tag helper can, through its properties, add additional attributes to the element that a developer can use to customize the tag's behavior.
A Tag Helper Component is a Tag Helper that allows you to conditionally modify or add HTML elements from server-side code. This feature is available in ASP.NET Core 2.0 or later. ASP.NET Core includes two built-in Tag Helper Components: head and body . They're located in the Microsoft.
If you wrap the new
expression in @(...)
it will all be treated as C# code so you won't have a problem.
Like this:
<website-information info="@(new WebsiteContext {
Version = new Version(1, 3),
CopyrightYear = "1638",
Approved = true,
TagsToShow = 131 })" />
Since my original answers was flawed, here is an approach that will work that I tested on code similar to yours:
If the CopyrightYear is a string what you can do is use single quotes for the outer quotes and use double quotes for the strings like so:
<website-information info='new WebsiteContext {
Version = new Version(1, 3),
CopyrightYear = "1638",
Approved = true,
TagsToShow = 131 }' />
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