[英]Saving array in session c# error
我正在嘗試將數組保存在會話中並試圖將其取回。 以下是代碼。 但是,當我調用WebMethod時出現以下錯誤。 我正在使用c#。 VS2010
錯誤:
System.NullReferenceException:對象引用未設置為對象的實例。 在C:\\ Users \\ uydarp \\ Documents \\ Visual Studio 2010 \\ Projects \\ xmlRW1 \\ xmlRW1 \\ Service1.asmx.cs中的xmlRW1.Service1.logic()中:第86行
[WebMethod]
public int logic()
{
int[] myArray = { 1,2,3,4};
Session["MyArray"] = myArray;
int[] myArray2 = (int[])Session["MyArray"];
int firstElement = myArray2[0];
return firstElement;
}
默認情況下, asmx
服務中的SessionState被禁用。 您可以通過更改WebMethod
屬性以顯式啟用它來啟用它:
[WebMethod(EnableSession = true)]
public int logic()
{
int[] myArray = { 1,2,3,4};
Session["MyArray"] = myArray;
int[] myArray2 = (int[])Session["MyArray"];
int firstElement = myArray2[0];
return firstElement;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.