【发布时间】:2011-10-06 17:58:14
【问题描述】:
我在我的 asp.net MVC 3 应用程序的 Model 文件夹中创建了一个类,并在其中使用了以下代码
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 控制器做什么?看起来您从未在其余代码中的任何地方使用过它...