Прежде всего, вам нужно создать фильтр, который вы можете использовать атрибут, чтобы контролировать, какие роли видят какие действия. Смотри http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs.
public class RequiresRoleAttribute : ActionFilterAttribute {
private List<string> requiredRoles = null;
/// <summary>
/// Initializes a new instance of the <see cref="RequiresRoleAttribute"/> class.
/// </summary>
/// <param name="roleNames">The role names.</param>
public RequiresRoleAttribute(params string[] roleNames) {
this.requiredRoles = new List<string>(roleNames);
}
/// <summary>
/// Called by the MVC framework before the action method executes.
/// </summary>
/// <param name="filterContext">The filter context.</param>
public override void OnActionExecuting(ActionExecutingContext filterContext) {
bool hasRole = false;
// check to see if the user has the proper role here
// if the do not have the role, they are not allowed to execute the action
if (!hasRole)
throw new UserAccessException("You do not have access to this action (" + filterContext.ActionDescriptor.ActionName + ", " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName + ")");
base.OnActionExecuting(filterContext);
}
}
Во-вторых, чтобы решить проблему отсутствия логики в представлениях, вы можете использовать дочерние действия для каждого раздела, для которого требуется роль. Опять же, вы можете применить свой фильтр к дочерним действиям. Подробнее о действиях с детьми см .: http://msdn.microsoft.com/en-us/library/ie/ee839451.aspx.
Что вам нужно изменить, так это раздел, который выдает исключение. Вам нужно проверить, является ли выполняемое действие дочерним. Если это так, вы захотите вернуть пустой контент.