I prefer to use action filters for Entitlement / Privilege logic. The beauty of these filters is they can run AFTER the action method populates your Model.
For Example:
public class AdminOnlyFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (!filterContext.Controller.ViewData.Model.IsMySiteAdminRole)
{
filterContext.Result = new ViewResult
{
ViewName = "NotEntitled",
Model = new NotEntitledViewModel(){Page = "[PageName]", SupportDG = "support@support.com"}
};
}
base.OnActionExecuted(filterContext);
}
}
Action Filters allow you to selectively override your Controller's OnActionExecuted
method.
This attribute can be applied to a specific action or an entire controller. The result will depend on your model values and will change your View only.