asp.net MVC3剃须刀:基于用户角色显示的ActionLink [英] asp.net MVC3 razor: display actionlink based on user role

查看:142
本文介绍了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

这篇关于asp.net MVC3剃须刀:基于用户角色显示的ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆