C# 未设置cookies值

C# 未设置cookies值,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,在我的项目中cookies值没有设置,我不明白为什么,哪里的问题我找不到。请帮助 [HttpPost] public ActionResult sellerLogin(string userid,string password) { int count = db.sellers.Count(p => p.userId == userid && p.password == password); if (count == 1)

在我的项目中cookies值没有设置,我不明白为什么,哪里的问题我找不到。请帮助

 [HttpPost]
    public ActionResult sellerLogin(string userid,string password)
    {

        int count = db.sellers.Count(p => p.userId == userid && p.password == password);
        if (count == 1)
        {
            HttpCookie selleruserid = new HttpCookie("selleruserid");
            selleruserid["selleruserid"] = userid.ToString();
            selleruserid.Expires = DateTime.Now.AddDays(220);
            Response.Cookies.Add(selleruserid);                
            return View("loginSuccess");
        }


        return View();
    }
登录成功()操作结果为

 public ActionResult loginSuccess()
    {
        HttpCookie selleruserid = Request.Cookies["selleruserid"];
        if (selleruserid != null)
        {
            ViewBag.userid = selleruserid["selleruserid"].ToString();

        } 


        return View();
    }
登录成功()查看包含

@ViewBag.userid

但是当控件转到LoginAccess时,viewbag显示为空…

请尝试使用
Response.SetCookie()
,因为
Response.Cookie.Add()
会导致添加多个Cookie,而
SetCookie
会更新现有Cookie


如果您添加逻辑来检查cookie是否已经存在,那么最好是
Response.SetCookie
else
Response.cookie.add
尝试使用
Response.SetCookie()
,因为
Response.cookie.add()
会导致添加多个cookie,而
SetCookie
会更新现有的cookie


如果您添加逻辑来检查cookie是否已经存在,则更好,然后
Response.SetCookie
else
Response.cookie.add
您好,您的代码对我来说运行良好,
如果您正在使用viewbag并且发生了任何重定向,那么您的viewbag将为空,您也可以使用此响应

您好,您的代码对我来说运行良好, 如果您正在使用viewbag并且发生了任何重定向,那么您的viewbag将为空,您也可以使用此响应

可能的重复可能的重复