我在我的asp.net MVC 3應用程序的Model文件夾中創建了一個類,並使用下面的代碼:非靜態方法或屬性'System.Web.MVC.ControllerContext.Controller.get'需要對象引用
var controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
但它有下劃線並說:一個對象引用需要用於非靜態方法或屬性
'System.Web.MVC.ControllerContext.Controller.get'
如何擺脫這個錯誤。
下面是完整的代碼:
public void OnAuthorization(AuthorizationContext filterContext)
{
var user = (CreditRegistryPrincipal)filterContext.HttpContext.User;
if (!user.IsAdminAuthorized)
{
var controller = System.Web.Mvc.ControllerContext ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
{ "controller", "Admin" },
{ "action", "adfdsf" }
});
}
}
問候, 阿西夫
你在使用var控制器做什麼?它看起來並不像你在其他任何代碼中的任何地方使用它... – Tommy