Home Replacement for @helper in ASP.NET 5 / asp.net mvc 6
Reply: 3

Replacement for @helper in ASP.NET 5 / asp.net mvc 6

Ifeanyi Chukwu
Ifeanyi Chukwu Published in 2015-09-01 12:04:03Z

I'm a huge fan of advancement and changes. It is clear to me that the ASP.NET MVC team have killed one of the most valuable feature of webpages (@helper) irrespective of what anybody thinks. If you must remove something, please replace it with something same in functionality and usage. So far, i don't think ViewComponent solves that neither does TagHelper. Is there any replacement to this? Something that takes parameters and returns a HtmlString.

No Nuget Package anywhere?

Some people call this a good improvement. I completely and strongly defer to that opinion.

I dont see anything harmful with:

@helper foo(string something){
     <div>Say @something</div>

var emailbody = classfilenameinAppCodefolder.foo("hello"); //store result in a variable for further processes

What on earth would make you delete such a valuable feature? What on earth can you replace that with?

For now i believe its a temporary delete before RC. https://github.com/aspnet/Razor/issues/281 and https://github.com/aspnet/Mvc/issues/1130 Well!!! it better be. I hope someone is working on it. Without @helper, building large HtmlString or 'template' would be a serious pain.

Note: Partial View doesnt seem to do the trick. I think it only renders views not return view to variable

Secondly, what happened to the App_Code folder?

Alexaku Reply to 2016-03-24 04:17:23Z
    Func<String, IHtmlContent> foo = @<div>Say @item</div>;
Henk Mollema
Henk Mollema Reply to 2015-09-08 15:12:06Z

The @helper directive was removed since it was incomplete and its current design did not fit in the new 'ASP.NET 5 way'. One of the reasons is that helpers should be declared in the App_Code folder while ASP.NET 5 has no concept of special folders. Therefore the team decided to temporarily remove the feature.

There are plans to bring it back in the future though. See this and this.

Gary Brunton
Gary Brunton Reply to 2017-01-16 22:53:20Z

I'd like to expand on @Alexaku's answer and show how I've implemented a helper like function. It's only useful on one specific page but it allows you to execute a piece of razor code multiple times with input parameters. The syntax is not great but I've found it very useful in the absence of razor's @helper function. First declare some kind of Dto that will contain the input parameters into the function.

@functions {
   private class Dto
      public string Data { get;set; }

Then declare the razor function. Note that the displayItem value can be multi-line and also note that you access the Dto variable using the @item.

   Func<Dto, IHtmlContent> displayItem = @<span>@item.Data</span>;

Then when you want to use the razor template you can call it like the following from anywhere in the page.

   @displayItem(new Dto {Data = "testingData1" });
   @displayItem(new Dto {Data = "testingData2" });
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.394525 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO