2014-02-22 137 views
1

我Put方法如下:最佳方式

public void Post([FromBody]RavenUserView view) 
     { 
      if (ModelState.IsValid) 
      { 
       var request = new CreateUserRequest(); 
       request.ID = view.ID; 
       request.Name = view.Name; 
       request.UserName = view.UserName; 
       request.Password = EncryptionDecryption.EncryptString(view.Password);//Encrypt The Password 
       request.Email = view.Email; 
       request.Phone = view.Phone; 
       request.Country = "x"; 
       request.Note = "y"; 
       request.IsActive = view.IsActive; 
       request.Creator = view.Creator; 
       request.CreationDate = DateTime.UtcNow; 
       request.ModificationDate = DateTime.UtcNow; 
       request.Remarks = "z"; 
       var response = _facade.CreateUser(request); 

       SaveUserDetailsToCookie(response.RavenUser.ID, response.RavenUser.UserName, EncryptionDecryption.DecryptString(response.RavenUser.Password));//Cookie should be stored Decrypted Format 
       HttpContext.Current.Session[SessionDataKey.UserId.ToString()] = response.RavenUser.ID.ToString(); 
       HttpContext.Current.Session[SessionDataKey.UserName.ToString()] = response.RavenUser.UserName; 
      } 
     } 

但是當我跑我的項目並嘗試保存我的信息,然後將它扔我一個異常「對象引用未設置爲對象」

我使用的網絡API作爲我的控制器類的引用。

閱讀各種文檔後,我發現Api是無狀態的。現在我如何儲存我的用戶信息以供進一步使用。

回答

2

用途:

public void Post([Bind(Include = "ID,Name,UserName,....")] CreateUserRequest request) 
    { 
     if(ModelState.IsValid) 
     { 
      var response = _facade.CreateUser(request); 

      SaveUserDetailsToCookie(response.RavenUser.ID, response.RavenUser.UserName, EncryptionDecryption.DecryptString(response.RavenUser.Password));//Cookie should be stored Decrypted Format 
      HttpContext.Current.Session[SessionDataKey.UserId.ToString()] = response.RavenUser.ID.ToString(); 
      HttpContext.Current.Session[SessionDataKey.UserName.ToString()] = response.RavenUser.UserName; 
     } 


    } 
+0

它會保持自己的值作爲會話? –

+0

它綁定您的模型,在視圖中。 – mirza