Allows you to write methods that contain mixed HTML and C# in Razor Views (.cshtml) / Razor Components (.razor) files.
This is a primitive for composing Razor code that is very low overhead / low concept. Your code calls your code.
In Views (not components): Can be async, can use tag helpers. In Components (not views): Usage and semantics still need figuring out, but we want to build something like this.
<h1>Here is some info about people on the team</h1>
@foreach(var person in people)
{
DisplayPerson(person);
}
@functions {
void DisplayPerson(Person person)
{
@<div>
<h3>
@person.Name is @person.Age
</h3>
@if (DateTime.Now.DayOfYear == person.Birthday.DayOfYear)
{
<h4>Happy Birthday! @person.Name</h4>
}
</div>
}
Person[] people = new Person[]
{
new Person()
{
Name = "Ryan",
Age = 33,
Birthday = new DateTime(1985, 9, 9),
},
new Person()
{
Name = "Dan", // Not Dan Roth, some other guy.
Age = 53,
Birthday = new DateTime(1965, 2, 3),
},
};
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime BirthDay { get; set; }
}
}
Is this W A V Y ? SPICY? Have you had cases where you wanted this? What would you add/change/remove?
To make a comparison as someone who went to the "dark side" of pure frontend development (but still doing a lot of C#, just more PAI and no Razor no more) I have to say there is a reason that vue.js is so successful and got traction: the .vue files just click with people. And the .vue files big thing: template markup, then script, then style tag, having everything in one place, making it a real component. Makes it much simpler to read the markup, too .
Sure, that is not the topic here, but just saying that having the option to just put markup in a Razor file at the top and markup-less C# code at the bottom with some binding-magic would solve sooth the "razor is a mess" fraction (I'm partially in), because.. well.. sorry.. having converted old Razor code to a modern frontend framework.. yeah, it's a mess. Or at least it's hard to read without a proper colorization, and often even with one.