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

  • SEARCH
  • Home
  • 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 7964049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:46:48+00:00 2026-06-04T05:46:48+00:00

I am relatively new to MVC3, and am developing a website that will need

  • 0

I am relatively new to MVC3, and am developing a website that will need to handle pre-loaded accounts in the default Microsoft membership provider, using SQL Server, EF4, etc. Some progress has been made, and with the help of someone on SO, I have got the ActionMethodSelectorAttribute working correctly to help me with that.

I.e., when we see someone’s ID as part of their attempt to load a profile page (www.mysite.com/profile/4) we will look to see if that ID/account has been ‘claimed’ or not. (My original posting is here: MVC3 using routes or using controller logic?)

Unfortunately, inside the ActionMethodSelectorAttribute, I am having a heck of a time doing a relatively simple database call to determine if the account is claimed/not claimed.

Here is my current state of the code:

    public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute
    {
        public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            // get profile id first
            int id = int.Parse((string)controllerContext.RouteData.Values["id"]);
            var profile = db.Profiles.Where(q => q.ProfileId == id).FirstOrDefault();
            bool isActivated = profile;// some code to get this state 
            return isActivated;
        }
    }

The line

var profile = db.Profiles.Where(q => q.ProfileId == id).FirstOrDefault();

errors on the db. section, with error message as follows:

Cannot access a non-static member of outer type ‘MySite.Controllers.HomeController’ via nested type ‘MySite.Controllers.HomeController.UserAccountActivatedAttribute’

…with the error being highlight under the db.

Does anyone know why, inside the ActionMethodSelectorAttribute, I cannot seem to make this call? (NOTE: inside the same Home controller, I am making many similar calls in Public ActionResult and ViewResult classes without any errors.)

EDIT

My HomeController.cs looks like this:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MySite.Models;

namespace MySite.Controllers
{
public class HomeController : Controller
{
    private MySiteEntities db = new MySiteEntities();

    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to MySite.com!";
        return View();
    }
    //several other ActionResults - create, delete, etc.

    public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute
    {
    public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        // get profile id first
        int id = int.Parse((string)controllerContext.RouteData.Values["id"]);
        var profile = db.Profiles.Where(q => q.ProfileId == id).FirstOrDefault();
        bool isActivated = profile;// some code to get this state 
        return isActivated;
    }
    }

…definitely it falls inside the Home Controller.

EDIT #2:

Closer, but a small issue with the value always being TRUE.

public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute
{
    private MySiteEntities db = new MySiteEntities();

    public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        int id = int.Parse((string)controllerContext.RouteData.Values["id"]);
        var data = new MySiteEntities();
        var claimed = db.Claimeds.FirstOrDefault(c => c.ProfileId == id);
        bool isActivated = claimed.Claimed1.Value != null;
        return isActivated;
    }
}

The claimed.Claimed1.Value != null; gives me a warning: The result of the expression is always ‘true’ since a value of type ‘bool’ is never equal to ‘null’ of type ‘bool?’

However, I have to have something there to handle a NULL value, right?

  • 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-06-04T05:46:49+00:00Added an answer on June 4, 2026 at 5:46 am

    I think your code actually looks more like this, am I right?

    public class HomeController : Controller
    {
        public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute
        {
            ...
        }
    }
    

    You need to make the attribute class a first-level class, not nested within the controller. You then need to give it its own db instance.

    public class HomeController : Controller
    {
        ...
    }
    
    public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute
    {
        private readonly CustomDbContext db = new CustomDbContext();
    
        public override bool IsValidForRequest(ControllerContext controllerContext, 
            MethodInfo methodInfo)
        {
            // original code here
        }
    }
    

    The reason for this is because when the attribute class is nested within the controller class, it cannot access the db instance variable, because it is not a static variable. Your attributes should really not be nested classes, and should have their own separate instance variables. In other words, don’t try to solve this by making the controller’s db variable static.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to the MVC world, and am developing an MVC3, C# .Net,
I am relatively new to Xcode and one thing that has bothered me is
Relatively new to JQuery. I've got some code that does a banner swap with
Note: I'm relatively new to MVC3. Input validation seems to be pretty nice with
Relatively new to python. I recently posted a question in regards to validating that
I'm brand new to .net MVC3 so pardon my ignorance. I have a relatively
I'm relatively new to jquery, so I have what I hope will be a
I am relatively new to MYSQL and have had an issue that has been
I'm relatively new to .NET and MVC3. I'm having some trouble with the above
I am relatively new to Objective C/iPhone Development. I am developing a simple app

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.