2013-12-14 25 views
-1

我在我的web应用程序中存储cookie以存储用户语言。 我在Global.asax文件创建在ASP.NET中的web应用程序中使用多个cookie

Application_BeginRequest && Application_Start

此cookie验证码:

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(); 
     //   } 
     //  } 
     // } 

但是当喜欢曲奇创建其他曲奇将消失,我会得到这个错误:

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 
+0

哪一行抛出这个错误? –

回答

0

我不知道这行抛出这个错误

,但可能我的客串线下方出现错误

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(); 
     //   } 
     //  } 

您需要检查不等于空

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

Before trying to get the value of a cookie, you should make sure that the cookie exists; if the cookie does not exist, you will get a NullReferenceException exception.

+0

让我说,我写了一个门户网站,接受模块,在我创建模块的项目,它工作正常,但是当我加载我的本地主机(核心门户)它显示该错误。 (事实上​​,我有2个项目1在Visual Studio中的其他一个在localhost文件夹中) –

相关问题