自定义过滤器属性和自定义授权属性执行

时间:2014-06-19 18:40:23

标签: asp.net-mvc fluent-nhibernate ninject

我有一个控制器范围的过滤器属性,如下所示:

public class LoggingNHibernateSessionAttribute : ActionFilterAttribute
    {

        private readonly IActionExceptionHandler _actionExceptionHandler;
        private readonly IActionTransactionHelper _actionTransactionHelper;

        public LoggingNHibernateSessionAttribute()
            : this(
            WebContainerManager.Get<IActionExceptionHandler>(),
            WebContainerManager.Get<IActionTransactionHelper>())
        {
        }

        public LoggingNHibernateSessionAttribute(
            IActionExceptionHandler actionExceptionHandler,
            IActionTransactionHelper actionTransactionHelper)
        {
            // _actionLogHelper = actionLogHelper;
            _actionExceptionHandler = actionExceptionHandler;
            _actionTransactionHelper = actionTransactionHelper;
        }

        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            // _actionLogHelper.LogEntry(actionContext.ActionDescriptor);
            _actionTransactionHelper.BeginTransaction();
        }

        public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
        {
            _actionTransactionHelper.EndTransaction(actionExecutedContext);
            _actionTransactionHelper.CloseSession();
            //_actionExceptionHandler.HandleException(actionExecutedContext);
            //_actionLogHelper.LogExit(actionExecutedContext.ActionContext.ActionDescriptor);
        }


    }

这是它的用法:

[LoggingNHibernateSession]
    public class BaseController : Controller
    {
}

我还有自定义授权属性:

 public class CustomAuthorizeAttribute : AuthorizeAttribute
    {

}

它是用法:

 [CustomAuthorize()]
        public ActionResult Index(
            int page = 1, int rows = 10)
        {
}

第一次执行操作时,LoggingNHibernateSessionAttribute中的方法运行,但是当我刷新页面时(仍然在该操作上)这次只有CustomAuthorize中的方法执行LoggingNHibernateSessionAttribute中的方法}不执行。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:-1)

我确实没有找到你,只有我能说,你可以覆盖MVC方法

    public interface IAuthenticationFilter
    {
    void OnAuthentication(AuthenticationContext filterContext);
    void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);
    }

以下是creating custom Authentication in MVC的链接。这必须帮助你......!