Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 从MVC中的全局变量访问值_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 从MVC中的全局变量访问值

Asp.net mvc 从MVC中的全局变量访问值,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,大家好,我们使用mvc4和jquery mobile开发web应用程序。在我们的每个控制器中,我们创建了如下所示的用户会话 公共类HomeController:BaseController { 私有静态用户CurrentUser public ActionResult Index(string name) { CurrentUser = (User)Session["CurrentUserSession"]; return View(); }

大家好,我们使用mvc4和jquery mobile开发web应用程序。在我们的每个控制器中,我们创建了如下所示的用户会话

公共类HomeController:BaseController { 私有静态用户CurrentUser

    public ActionResult Index(string name)
    {
       CurrentUser = (User)Session["CurrentUserSession"];
       return View();
    }

   public ActionResult UserDetaiks()
    {
       string username = CurrentUser.UserFName;
       return View()
     }
}

谢谢各位,还有一个疑问

我在表单authenticate中使用了下面的代码

 HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authCookie != null)
                {

                    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

                    var s = new System.Web.Script.Serialization.JavaScriptSerializer();
                    User obj = s.Deserialize<User>(authTicket.UserData);

                    UserInformation CurrentUser = new UserInformation(obj.UserID);
                    CurrentUser.UserId = obj.UserID;
                    CurrentUser.FirstName = obj.UserFName;
                    CurrentUser.LastName = obj.UserLName;
                    CurrentUser.roles = obj.SecurityGroupName;
                    CurrentUser.DefaultWarehouseId = obj.DefaultWhseIdentity; eg:14
                    HttpContext.Current.User = CurrentUser;

                }



**Here User can Change CurrentUser.DefaultWarehouseId  later like CurrentUser.DefaultWarehouseId = 16. but when i leave the method. It again getting value 14. Now i can code so CurrentUser.DefaultWarehouseId will be 16 through out app one i changed, Please guide me.**
HttpCookie authCookie=Request.Cookies[FormsAuthentication.formscookeName];
if(authCookie!=null)
{
FormsAuthenticationTicket authTicket=FormsAuthentication.Decrypt(authCookie.Value);
var s=new System.Web.Script.Serialization.JavaScriptSerializer();
User obj=s.Deserialize(authTicket.UserData);
UserInformation CurrentUser=新用户信息(obj.UserID);
CurrentUser.UserId=obj.UserId;
CurrentUser.FirstName=obj.UserFName;
CurrentUser.LastName=obj.UserLName;
CurrentUser.roles=obj.SecurityGroupName;
CurrentUser.DefaultWarehouseId=obj.DefaultWhsIdentity;例如:14
HttpContext.Current.User=CurrentUser;
}
**在此,用户可以稍后更改CurrentUser.DefaultWarehouseId,如CurrentUser.DefaultWarehouseId=16。但是当我离开这个方法的时候。它又一次获得了14的价值。现在我可以编码,所以CurrentUser.DefaultWarehouseId将是16,通过我更改的应用程序1,请指导我**

由于无法保证在调用UserDetails方法之前调用Index方法,因此我不会使用静态变量来存储用户。相反,每个控制器方法应根据需要从会话中获取用户。

在构造函数中初始化:

public class HomeController : BaseController
{
    private User CurrentUser;

    public HomeController()
    {
        CurrentUser = Session["CurrentUserSession"] as User;
    }

    public ActionResult Index(string name)
    {       
       return View();
    }

    public ActionResult UserDetaiks()
    {
        string username = CurrentUser.UserFName;
        return View()
     }
 }

在我看来,这个问题更适合CodeReview,但现在我们来看:

代码要求用户首先访问
索引
,为当前用户变量赋值。如果用户首先访问详细信息,您将得到一个
NullReferencEception

最后,如果您不需要在每个方法中都使用ActionMethod,那么最好在ActionMethod中帮助当前用户。如果在每个操作中都需要,可以在构造函数中初始化它

public class HomeController : BaseController
{
    public ActionResult Index(string name)
    {
       User CurrentUser = (User)Session["CurrentUserSession"];
       return View();
    }

    public ActionResult UserDetails()
    {
        User CurrentUser = (User)Session["CurrentUserSession"];
        string username = CurrentUser.UserFName;
        return View()
    }
 }
或:


在任何情况下,都应该检查会话变量是否不为null。我想如果没有用户登录,这可能会很糟糕。

封装当前用户逻辑的另一种方法-使用自定义ModelBinder,例如:

public class UserBinders : System.Web.Mvc.IModelBinder
{
       private const string SessionKey = "CurrentUser";

       public object BindModel(ControllerContext controllercontext, System.Web.Mvc.ModelBindingContext bindingContext)
       {  
          HttpContextWrapper httpcontext = new HttpContextWrapper(System.Web.HttpContext.Current);
          string userLogin = httpcontext.User.Identity.Name

          var currentUser = (User)controllercontext.HttpContext.Session[SessionKey];
          if (currentUser == null)
          {
              currentUser = context.GetUserByLogin(userLogin);
              controllercontext.HttpContext.Session[SessionKey] = currentUser;
          }

          return currentUser;
       }
}
只需声明变量类型User,并获取当前用户

public ActionResult UserDetaiks(User CurrentUser)
{
   string username = CurrentUser.UserFName;
   return View()
 }

为什么要创建全局?如果需要,您可以在本地操作中从会话中获取它。此外,如果您直接访问详细信息,而不首先访问索引,则没有CurrentUser,因为没有分配。您是否与两个不同的用户尝试过此操作?使用
static
无效。请记住,
static
不是每个请求都可以使用的。它实际上是在应用程序池的整个生命周期中共享的。简而言之,不要这样做。在你最初的问题解决之后,请不要扩展你的问题。如果您有其他问题,请打开单独的问题,但如果我需要从会话中获取用户。然后我不得不写很多冗余代码。将来的更改将非常困难。您可以将一些代码放入基类(从控制器派生),基类从会话检索用户并将其放入属性中,例如。然后从基类派生UserController。根据您的场景,另一个选项可能是使用ActionFilter.thaks bro im应用构造函数方法。请参阅我更新的帖子:我在构造函数中遇到null异常。我在基本控制器中创建了会话。在home controller构造函数中,显示会话为空。非常感谢。还有一个疑问。我在web应用程序中使用了表单身份验证。在global.asax应用程序中,我使用了下面的代码。UserInformation CurrentUser=新用户信息(obj.UserID);CurrentUser.FirstName=obj.UserFName;CurrentUser.DefaultWarehouseId=obj.DefaultWhseIdentity.ToString();HttpContext.Current.User=CurrentUser;在此,用户可以随时更改CurrentUser.DefaultWarehouseId。但我无法在全局外部赋值。asx:m在构造函数内部遇到null异常。我在基本控制器中创建了会话。在home controller构造函数中,它显示的会话为空。您和您都不想为此使用
static
。当多个用户使用它时,它会爆炸(或者说当前用户将被覆盖)。您已尝试编辑我的答案。如果您想对它进行注释,请使用comment函数。我在构造函数中得到null异常。我在基本控制器中创建了会话。在home controller构造函数中,它显示会话为空。您可能想再次阅读我的最后一句话。如果事先没有在会话中存储内容,那么会话对象自然为null。
public class UserBinders : System.Web.Mvc.IModelBinder
{
       private const string SessionKey = "CurrentUser";

       public object BindModel(ControllerContext controllercontext, System.Web.Mvc.ModelBindingContext bindingContext)
       {  
          HttpContextWrapper httpcontext = new HttpContextWrapper(System.Web.HttpContext.Current);
          string userLogin = httpcontext.User.Identity.Name

          var currentUser = (User)controllercontext.HttpContext.Session[SessionKey];
          if (currentUser == null)
          {
              currentUser = context.GetUserByLogin(userLogin);
              controllercontext.HttpContext.Session[SessionKey] = currentUser;
          }

          return currentUser;
       }
}
public ActionResult UserDetaiks(User CurrentUser)
{
   string username = CurrentUser.UserFName;
   return View()
 }