4

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.

Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378

2 Answers2

5

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"))
}
  • http://stackoverflow.com/questions/4451287/razor-declarative-html-helpers/4451843#4451843 recommends to pass page: `@helper Table(WebViewPage page)` Is this better, no need for two parameters, Razor view code is shorter and easier to read ? How to remote this parameter like in extension method where `this` can accessed automatically ? – Andrus May 04 '15 at 06:08
  • Can't see why not - `WebViewPage` exposes both `Html` and `Url` as public properties (you would just need to change it to `page.Html.Raw(page.Url.Action(..))` However `@helper` was not really designed for this usage and my recommendation would be to use a custom HtmlHelper extension method (which has the added benefit of being able to compile it in a separate dll and reference it into multiple projects) –  May 04 '15 at 06:22
  • Helper contains lot of html code. If custom extension is used, should html created using StringBuilder? How to use VS html editing, syntax highlight, check and auto formatting features in this case to create helper? – Andrus May 04 '15 at 08:07
  • 1
    When you say the _Helper contains lot of html code_, do you mean your helper (and you just simplified your question)? Can you give me an example of some other html your generating. Also [here](http://stackoverflow.com/questions/26955073/converting-asp-net-mvc-razor-helper-function-into-a-method-of-a-helper-class/26955246#26955246) and [here](http://stackoverflow.com/questions/29710876/helper-for-a-labelfor-which-wraps-an-html-tag/29711163#29711163) are some examples of creating HtmlHelpers –  May 04 '15 at 08:19
  • My html is similar in the Html examples you provides. I*m prefer to use VS html editor instead of using C# code to create them. So I will use Razor helpers and duplicate same helper files from App_code folder in all projects. Html editor marks those helpers with red underline after html is changed sometimes. How to fix this, can razon App_code helpers assembly referenced in web.config or other way. – Andrus May 04 '15 at 09:26
1

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"))
}