0

我对MVC3相对来说比较新,并且正在开发一个网站,该网站需要使用SQL Server,EF4等来处理默认Microsoft成员资格提供程序中的预加载帐户。取得了一些进展,并且在某人的帮助下在所以,我已经得到ActionMethodSelectorAttribute正确工作,以帮助我。ActionMethodSelectorAttribute无法访问类型?

也就是说,当我们看到某人的ID是他们尝试加载个人资料页面(www.mysite.com/profile/4)的一部分时,我们会查看该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(); 

错误。部分,并显示错误消息如下:

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

访问外类型“MySite.Controllers.HomeController”的非静态成员。 ..与数据库下的高亮错误。

有没有人知道为什么,在ActionMethodSelectorAttribute中,我似乎无法进行此调用? (注:同一家庭控制器里面,我在公众面前做的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; 
    } 
} 

claim.Claimed1.Value!= null;给我一个警告:由于类型'bool'的值永远不等于'bool'类型的'null',因此表达式的结果总是'真'。

但是,我必须有东西来处理NULL值,对吧?

+0

'bool'永远不能为空。只有'bool?'可以为null。是声称.Claimed1.Value'bool'或'bool?'类型..?如果它是'bool',则不需要空检查,因为它不能为空。如果它是'bool?'另一方面,使用这个检查null:'bool isActivated = claims.Claimed1.Value.HasValue;' – danludwig

+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来解决这个问题。

+0

啊哈!我不知道。我将编辑我的问题以显示它的实际外观,然后尝试调查此问题。谢谢,丹! –

+0

我必须确保仍然在HomeController中但依赖于ActionMethodSelectorAttribute的一些ActionResults仍然可以工作。 [ActionName(“Profile”)] public ActionResult IndexNonActivated(int?id) { return RedirectToAction(“Claimed”,new {id = id}); } –

+0

ActionMethodSelectorAttribute不必与控制器位于同一个类或文件中。在action方法中使用'[UserAccountActivated]'语法将一个属性应用于控制器。回去并重新阅读这个:http://stackoverflow.com/a/10613782/304832 – danludwig