I'm 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 doesn't seem to work in razor.
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?
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();
}
If you want a code block, that would do in the view :
I've modified Richard's code to provide an optional MvcHtmlString parameter to return if the evaluation is false.
The solution suggested by Richard is really beautiful, though as Matthieu noted someone might need extra html code to be rendered (or not rendered) as well. Thus Matthieu's solution seems to be more widely applicable, I would just centralize the logic regarding which users are considered admins in extension method.
Extension method:
Usage:
I have in the past created a helper function to only return output when a criteria is met like this:
so you can use this:
This way it is legible and short
If you haven't enabled
roleManager
and you still want to check User's role, you can do it like this:What we are doing it here is, we are just accessing user's properties. This can useful if you are managing roles yourself.
You can add a function to App_Code/ViewFunctions.cshtml (create if missing)
To use that function just add following code to the view.