开发者

an object reference is required for non static method or property 'System.Web.MVC.ControllerContext.Controller.get'

I have created a class in my asp.net MVC 3 application's Model folder and using the following code in it

   var controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue

but it is underlined and says: an object reference is required for non static method or property

'System.Web.MVC.ControllerContext.Controller.get'

how to get ri开发者_开发知识库d of this error.

Below is full code:

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" } 
                });
            }
        }

Regards, Asif


This line:

var controller = System.Web.Mvc.ControllerContext ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;

isn't syntactically valid, so I'd expect a different compilation error (; expected).

Not sure why you're trying to access ViewContext inside a model, though. Presumably, it's actually a custom authorization attribute?

Have you tried:

var controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;

or

System.Web.Mvc.ControllerContext controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;


You need to use the filtercontext that is passed to the OnAuthorization function. So you should be able to change this:

var controller = System.Web.Mvc.ControllerContext ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;

To This:

var controller = filterContext.Controller.ValueProvider.GetValue("controller").RawValue;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜