0
votes

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
bool can never be null. Only bool? can be null. Is claimed.Claimed1.Value a bool or a bool? type..? If it is bool, you do not need a null check, since it can never be null. If it is a bool? on the other hand, check null using this: bool isActivated = claimed.Claimed1.Value.HasValue; - danludwig
Somehow I missed this, but I will use it as well. - edward_h

1 Answers

1
votes

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.