如何在 App_Code 文件夹中的 Razor 视图中使用 Url.Action

How to use Url.Action in Razor view in App_Code folder

如何在 App_Code 文件夹中的 Razor 助手中使用 Url.Action()?

我按照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"))
}

但是出现编译错误

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

ASP.NET 使用了 MVC4 和 jquery。

Html.Raw() 使用 HtmlHelper class 但 Url.Action() 使用 UrlHelper Class 所以你也需要传递

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

这对我也有效,无需传递参数:

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