4
votes

How to use Url.Action() in Razor helper from App_Code folder ?

I tried according to Why I cant use Html.RenderPartial in razor helper view File in App_Code Folder?

@using System.Web.Mvc.Html
@helper Tabel(System.Web.Mvc.HtmlHelper html)
{ 
    @Html.Raw(Url.Action("Index", "Home"))
}

but got compile error

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

ASP.NET MVC4 and jquery are used.

2

2 Answers

5
votes

Html.Raw() uses the HtmlHelper class but Url.Action() the UrlHelper Class so you would need to pass that as well

@using System.Web.Mvc
@helper Tabel(HtmlHelper html, UrlHelper url)
{ 
    html.Raw(url.Action("Index", "Home"))
}
1
votes

This worked for me too without passing the arguments:

@helper Tabel()
{ 
    var html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;
    var url = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Url;

    html.Raw(url.Action("Index", "Home"))
}