Skip to content

Discussion: bringing back @helper for views #5110

Closed
@NickCraver

Description

@NickCraver

In porting some existing applications (from ASP.NET MVC 5), we're seeing immense amounts of pain with views that use @helper today. @helper, from the user view point, is a very simple way to use Razor templating to re-use some code. Perhaps to render a date a certain way, or a chart, or a graph, or whatever. As an example, Stack Overflow's codebase has 458 @helpers today.

Some history:

But @helper still lacks an equally (or even close) usable replacement. The alternatives I'm aware of today are:

  • Tag Helpers which involve writing C# per helper
  • View Components which involve writing C# and a view per helper
  • Using a dynamic workaround which isn't type safe and I'm not sure on the performance implications of
  • Build the string yourself, and hope you encode everything correctly

There are 2 ways @helper worked in ASP.NET MVC 5: globally from App_Data and in individual views. I am only advocating for support of the latter. IMO, Tag Helpers or view components (pick your flavor!) are excellent replacements for the global use case. That makes total sense and having another view file is also reasonable.

Let's say we have a view today with 5 @helper sections in ASP.NET MVC 5. In ASP.NET Core, it's a regression. We have a few options:

  • Copy the code everywhere
  • Write a function rendering HTML strings (basically no Razor or safety for the helper)
  • Have a child view per helper, going from 1 view to 6, and 1 file to 6
  • Create a tag helper for each (5 classes...and again mostly removing HTML safety, or suffering overhead)
  • Use the dynamic approach, eat the performance, and hope it doesn't go boom at runtime

Note: I am not sure about the performance differences on these. That needs to be tested. I'll try and get benchmarks going when time allows. I have a hard time imagining any of these would be as fast as what @helper used to do, though. But I take nothing for a given here...we need to benchmark, even if @helper isn't a candidate yet.

A lot of users have chimed in on closed issues above. A lot of migration pain is being felt. Probably much more than was envisioned when @helper was removed. This is one of the largest issues I see in our migrations and it's something I'm really feeling right now. Is there a possibility we bring @helper or equivalent functionality (re-using some Razor templating inside the same view) back?

cc @DamianEdwards @rynowak

Metadata

Metadata

Labels

Components Big RockThis issue tracks a big effort which can span multiple issuesDoneThis issue has been fixedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-razor-pages

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions