How to use Url.Action in a Helper Razor?

0

In an ASP.NET MVC5 project, I created a Helper inside the App_Code folder, however, when I use the Url.Action() function on my Helper the following compile error occurs:

  

CS0103: The name 'Url' does not exist in the current context

My code:

@helper testeHelper()
{
    <a href="@Url.Action("Index", "Home")" >teste</a>
}
    
asked by anonymous 03.09.2015 / 16:17

1 answer

1

Within Helper we do not have access to Helpers defaults, to use them it is necessary to pass as a parameter.

The code for Helper looks like this:

@using System.Web.Mvc
@helper testeHelper(UrlHelper Url)
{
    <a href="@Url.Action("Index", "Home")" >teste</a>
}

And in its View it does so to call Helper :

@MeusHelpers.testeHelper(Url);
    
03.09.2015 / 16:17