2012-10-05 58 views
0

正如我試圖通過使用QueryString作爲要評估的數據來測試/調試我的應用程序 。更改會話參數/變量

我是想通過會話變量來實現相同的方法在我other post regarding QueryStrings

我正在尋找一種方法來操縱 通過輔助方法,查詢字符串..

現在我是想繼續前進,triyng使用相同的方法,與查詢字符串

代碼會話變量同樣的事情是

public static class Sesion 
    { 
    public sealed class Act 
    { 
    public const string edit = "edit", add = "add", remove = "remove", replace = "replace"; 
    } 

      public static void Modify(string action, string New_Value, string CurSes_ParamName, string NewSession_paramName = null, bool redirectWithNewQuerySettings = false) 
      { 

       #region <<=========== reflect to readonly & set QueriString ReadOnly - false ============>> 



       //HttpContext.Current.Request.QueryString["qs_key"]; 

       // reflect to readonly property 
       PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); 

       // make collection editable 
       isReadOnly.SetValue(HttpContext.Current.Session, false, null); 

       #endregion 
       switch (action) 
       { 
        case Act.remove: 
         if (itsNotAnEmptySession()) 
          HttpContext.Current.Session.Remove(CurSes_ParamName); 
         break; 
        case Act.replace: 
         HttHttpContext.Current.Session.Remove(CurSes_ParamName); 
         HttpContext.Current.Session.Add(NewSession_paramName , New_Value); 
         break; 
        case Act.edit: 
         HttpContext.Current.Session.Set(CurSes_ParamName, New_Value); 
         break; 

        case Act.add: 
         HttpContext.Current.Session.Add(NewSession_paramName , New_Value); 
         break; 

       } 



       isReadOnly.SetValue(HttpContext.Current.Session, true, null); 

      } 
     } 
} 

錯誤我試圖設置只讀值爲false的錯誤: ,因爲我仍然沒有足夠的經驗與.net我不明白 我去哪裏錯了或...是完全不可能的在會話變量上實現這一點。

Object does not match target type.

嘗試它的查詢字符串,並用這種方法工作是罰款,在這行代碼:

isReadOnly.SetValue(HttpContext.Current.Request.QueryString, false, null); 

回答

2

你不能只是做同樣的會議:

第一,Http.Current.Session不是NameValueCollection(如QueryString),而是HttpSessionState(它解釋了您的Object does not match target type消息)

然後,HttpSessionState有一個IsReadonly屬性,但是它是隻讀的(不能設置它)。當從NameValueCollectionIsReadonly屬性可設置。

所以...沒辦法(至少這種方式)。

+0

感謝您的解釋。我只是嘗試'Session.Add(param,value)'這工作正常沒有問題添加到集合,雖然想找到編輯一個值,從集合中刪除項目,將有可能只有先刪除,添加新(相同的parName ),具有新的價值? – LoneXcoder

+0

我想我已經找到了應用程序(狀態變量)的完美解決方案的任何缺點?它可以讓您乍看之下所需的任何東西 – LoneXcoder