Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:44:03+00:00 2026-05-26T02:44:03+00:00

I have created a class in my asp.net MVC 3 application’s Model folder and

  • 0

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 rid 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:44:03+00:00Added an answer on May 26, 2026 at 2:44 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET MVC 3 (Razor) and have created a Controller named Controllers -
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.
I am creating a modular ASP.NET MVC application using areas. In short, I have
I have an ASP.NET MVC (using the release candidate) application that works with a
I have a pretty big web application that I created last year using ASP.NET
using VS 2008 i created a clean asp.net MVC application project and changed nothing
i have asp.net mvc application where my model has a relation like Question can
I created an asp.net mvc 3 facebook application using facebook c# sdk. I'm trying
I am working on an asp.net application and I have created a folder named
I have an ASP.Net MVC application with a model which is several layers deep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.