我已经创建了一个类在我的asp.net MVC 3应用程序的模型文件夹和使用以下代码在它
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 controller = System.Web.Mvc.ControllerContext ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;
在语法上是无效的,所以我预计会出现不同的编译错误(;预计)。
不知道为什么你试图访问一个模型内的ViewContext,虽然。据推测,它实际上是一个自定义授权属性?
你试过了吗?
var controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;
或
System.Web.Mvc.ControllerContext controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;
您需要使用传递给OnAuthorization函数的filtercontext。所以你应该可以修改这个:
var controller = System.Web.Mvc.ControllerContext ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;
:
var controller = filterContext.Controller.ValueProvider.GetValue("controller").RawValue;