asp.net MVC3剃须刀:基于用户角色显示的ActionLink
进出口新的MVC。我希望能够隐藏某些actionlinks一些用户。说我有一个创造的ActionLink,我只希望管理员看到并点击。我想用某种显示LoggedInTemplate在asp.net中可用,但它似乎在剃刀工作..
Im new to MVC. I want to be able to hide some actionlinks for some users. Say I have a "create" actionlink which I only want administrators to see and click. I want to use some sort of "loggedintemplate" available in asp.net, but it doesnt seem to work in razor..
我可以用某种code块与if语句检查当前用户和她的角色,不过这可能不是最好的做法?
I could use some sort of code block with an if statement checking the current user and her role, however that may not be best practice?
我index.cshtml ..
my index.cshtml..
// want some adminauth attribute here...
@Html.ActionLink("Create New", "Create")
我的控制器..
my controller..
// GET: /Speaker/Create
[Authorize(Roles = "Administrators")]
public ActionResult Create()
{
return View();
}
干杯! :)
我在过去创造了一个辅助函数来只有当条件满足这样返回的输出:
I have in the past created a helper function to only return output when a criteria is met like this:
public static MvcHtmlString If(this MvcHtmlString value, bool evaluation)
{
return evaluation ? value : MvcHtmlString.Empty;
}
所以你可以使用这样的:
so you can use this:
@Html.ActionLink("Create New", "Create").If(User.IsInRole("Administrators"))
这事情是这样的清晰,短
This way it is legible and short