ActionMethodSelectorAttribute无法访问类型?

时间:2012-05-19 19:35:32

标签: asp.net-mvc-3 entity-framework class controller asp.net-membership

我对MVC3相对较新,并且正在开发一个网站,需要使用SQL Server,EF4等处理默认Microsoft成员资格提供程序中的预加载帐户。已经取得了一些进展,并且在有人在SO上,我已经让ActionMethodSelectorAttribute正常工作来帮助我。

即,当我们在尝试加载个人资料页面(www.mysite.com/profile/4)时看到某人的ID时,我们会查看该ID /帐户是否已“声明”。 (我原来的帖子在这里:MVC3 using routes or using controller logic?

不幸的是,在ActionMethodSelectorAttribute中,我有一段时间做一个相对简单的数据库调用,以确定该帐户是否已声明/未声明。

这是我目前的代码状态:

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

该行

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

db上的错误。部分,错误消息如下:

  

无法通过嵌套类型'MySite.Controllers.HomeController.UserAccountActivatedAttribute'

访问外部类型'MySite.Controllers.HomeController'的非静态成员

...错误在数据库下突出显示。

有没有人知道为什么,在ActionMethodSelectorAttribute中,我似乎无法进行此调用? (注意:在同一个Home控制器中,我在Public ActionResult和ViewResult类中进行了许多类似的调用,没有任何错误。)

修改

我的HomeController.cs看起来像这样:

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

......绝对属于家庭控制器。

编辑#2:

更近,但值很小的问题始终为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;
    }
}

声明。声明1.Value!= null;给我一个警告:表达式的结果总是'true',因为'bool'类型的值永远不等于'bool'类型的'null'

但是,我必须要有一些东西来处理NULL值,对吗?

1 个答案:

答案 0 :(得分:1)

我认为你的代码看起来更像是这样,我是对的吗?

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

您需要将属性类设为第一级类,而不是嵌套在控制器中。然后,您需要为其提供自己的数据库实例。

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

这是因为当属性类嵌套在控制器类中时,它无法访问db实例变量,因为它不是静态变量。您的属性应该不是嵌套类,并且应该有自己独立的实例变量。 换句话说,不要试图通过使控件的db变量static来解决此问题。