Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@helper directive no longer works out of the box in ASP.NET5 MVC6 beta4.

Since I updated my ASP.NET5 project to beta4 (the one included with Visual Studio 2015 RC), any of my Razor views where I use a helper, such as:

@helper foo()
{
   <h2>Bar</h2>
}

results in the following error:

error CS0103: The name 'helper' does not exist in the current context.

Is the @helper directive no longer supported? Can someone point me to anything useful about the issue?

like image 320
Gabor Avatar asked Apr 30 '15 14:04

Gabor


3 Answers

In .Net Core and above you can use the function derivative to achieve the same purpose:

@functions {
    private void RenderFoo()
    {
        return "<h2>Bar</h2>";
    }
}

<div>From method: @{ RenderFoo(); }</div>
like image 115
ajbeaven Avatar answered Sep 28 '22 05:09

ajbeaven


Rather than using a helper method, you can achieve the same functionality using a partial with a view model. Just pass the relevant arguments into the Html.Partial command.

like image 34
Lucent Fox Avatar answered Sep 28 '22 05:09

Lucent Fox


The @helper directive was removed since beta 4 because it imposed too many restrictions on other Razor features: https://github.com/aspnet/Razor/issues/281.

Edit
To be clear: based on the discussion in the GitHub issue(s) Microsoft is not planning to bring the @helper directive back or replace it in ASP.NET Core.

like image 45
Henk Mollema Avatar answered Sep 28 '22 05:09

Henk Mollema