How to use Url.Action in a Razor Helper?

Asked

Viewed 955 times

1

In an ASP.NET MVC5 project, I created a Helper inside the briefcase App_Code, however, when using the function Url.Action() in my Helper the following build 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>
}

1 answer

2


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

The code of Helper, gets like this:

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

And in his View does so to call the Helper:

@MeusHelpers.testeHelper(Url);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.