2

我正在尝试更改方法OnAuthorization,以便它可用于任何应用程序......这样:

public partial class Controller
{
    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        if ((string)(filterContext.RouteData.Values["action"]) == "test")
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }

}

但显示编译错误:

Controller.OnAuthorization(System.Web.Mvc.AuthorizationContext)':找不到合适的方法来覆盖

有人能帮我吗?

4

1 回答 1

1

You have to create you own base controller class:

public partial class BaseController : Controller
{
    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        if ((string)(filterContext.RouteData.Values["action"]) == "test")
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }
}

Use BaseController in your code.

And remember that filterContext.RouteData.Values["action"] can be Test or TEST or tEST.

于 2010-04-26T15:59:05.323 回答