我有许多权限,并根据一组条件,这些权限确定用户是否可以看到某些功能。我为此编写了一个辅助函数,因为视图中的逻辑变得非常广泛。
基本上我正在寻找一个与Html.ActionLink相同的功能,我可以从类文件中访问(理想情况下,如果我可以访问那个很棒的Helper)那么我可以这样做,
public static string GetAdminLinks()
{
if(PermCheck)
{
return(Html.ActionLink(...));
}
}
任何sugestions?
答案 0 :(得分:5)
:
Url.Action("Index", "Home", null, Request.Url.Scheme);
答案 1 :(得分:3)
这在很大程度上取决于您的权限检查的实现方式(以及确定用户权限所需的信息)。无论如何,我将它实现为HtmlHelper类的扩展。
App_Code中的某个地方:
using System.Web.Mvc.Html;
public static class HtmlHelperExtensions {
public static string SecureActionLink(this HtmlHelper htmlHelper, string action, string controller){
if(PermCheck)
return htmlHelper.ActionLink(action, controller);
else
return string.Empty;
}
//add other ActionLink overrides if you like...
}
然后,您就可以在ViewPages中的任何位置调用扩展方法,而无需任何代码。