I would like to provide an overview answer to simplify this post.
The closest approach to the question is @Vitox's answer, but lacks how to call the method in place:
@{
void MakeNote(string content)
{
<p><strong>Note</strong> @content </p>
}
}
This is good for helpers with a lot of C# code and is called like this:
<div>
<p>Today is a great day for programming.</p>
@{MakeNote("Have a nice day!");}
</div>
In case you don't like the @{Method(params);} construct, I suggest @Shaun Luttin's answer:
Func<string, IHtmlContent> MakeNote =
@<p><strong>Note</strong> @item </p>;
}
This is good for HTML oriented helpers and is called like this:
<div>
<p>Today is a great day for programming.</p>
@MakeNote("Have a nice day!")
</div>
I don't know why Shaun wrote {@item} in his code, but I did notice that sometimes VS 2022 requires cleaning up the project to compile such code.
Here is an extended example with this second approach:
Func<ValueTuple<int, string>, IHtmlContent> SomeMethod =
@<p>
@{int x = item.Item1 + 2;}
<strong>Note</strong> @(item.Item2 + x)
</p>;
When calling like this @SomeMethod((5, "Have a good day!")) the following output is displayed:
<p>
<strong>Note</strong> Have a good day!7
</p>