簡體   English   中英

將數組保存在會話C#錯誤中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM