Generating HTML output using a code-only class approach
works – but is not ideal.
One of the features we are looking to enable with Razor is
an easy way to create re-usable HTML helpers using a more declarative
approach. Our plan is to enable you to define reusable helpers using a
@helper { } declarative syntax like below.
You’ll be able to place .cshtml files that contain these
helpers into a Views\Helpers directory and then re-use them from any view or
page in your site (no extra steps required):
Note above how our ProductListing() helper is able to define
arguments and parameters. This enables you to pass any parameters you
want to them (and take full advantage of existing languages features like
optional parameters, nullable types, generics, etc). You’ll also get
debugging support for them within Visual Studio.
Note: The @helper syntax won’t be in the first beta of Razor
– but is something we hope will be enabled with the next drop. Code-based
helpers will work with the first beta.