【问题标题】:using multiple cookies in a web application in ASP.NET在 ASP.NET 的 Web 应用程序中使用多个 cookie
【发布时间】:2013-12-15 02:43:30
【问题描述】:

我的网络应用程序中有 cookie 来存储用户语言。 我在

中创建了这个cookie

Application_BeginRequest && Application_Start

在我的 global.asax 文件中使用此代码:

Response.Cookies["IPortalCookies"]["Language"] = "en";
                        Response.Cookies["IPortalCookies"]["Direction"] = "ltr";
                        Response.Cookies["IPortalCookies"].Expires = DateTime.Now.AddYears(1);

现在我需要为每个用户创建另一个 cookie 来存储喜欢:

int articleid = Convert.ToInt32(e.CommandArgument);
        //    var _ah = new ArticlesHelper();
        //    if (Request.Cookies["IPortalCookies"] != null)
        //    {
        //        if (Request.Cookies["IPortalCookies"]["Likes"] == null || Request.Cookies["IPortalCookies"]["Likes"].Contains("'" + articleid + "'") == false)
        //        {
        //            if (_ah.LikeIt(articleid))
        //            {
        //                Response.Cookies["IPortalCookies"]["Likes"] = Request.Cookies["IPortalCookies"]["Likes"] + ",'" + articleid + "'";
        //                BindRepeater();
        //            }
        //        }
        //    }

但是当点赞 cookie 创建其他 cookie 时会消失,我会得到这个错误:

String reference not set to an instance of a String.
Parameter name: name

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: name

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentNullException: String reference not set to an instance of a String.
Parameter name: name]
   System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride) +14174389
   PortalCore.Global.Application_BeginRequest(Object sender, EventArgs e) +1093
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

【问题讨论】:

  • 哪一行抛出了这个错误?

标签: c# asp.net cookies


【解决方案1】:

我不知道是哪一行抛出了这个错误

但是我猜下面一行会出错

if (Request.Cookies["IPortalCookies"]["Likes"] == null || Request.Cookies["IPortalCookies"]["Likes"].Contains("'" + articleid + "'") == false)
        //        {
        //            if (_ah.LikeIt(articleid))
        //            {
        //                Response.Cookies["IPortalCookies"]["Likes"] = Request.Cookies["IPortalCookies"]["Likes"] + ",'" + articleid + "'";
        //                BindRepeater();
        //            }
        //        }

你需要检查不等于null

Request.Cookies["IPortalCookies"]["Likes"] != null

在尝试获取 cookie 的值之前,您应该确保 cookie 存在;如果 cookie 不存在,你会得到一个 NullReferenceException 异常。

【讨论】:

  • 让我说,我写了一个接受模块的门户,在我正在创建模块的项目中它工作正常,但是当我加载我的本地主机(核心门户)时它显示该错误。 (实际上我有 2 个项目 1 在 Visual Studio 中,另外一个在 localhost 文件夹中)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多