2015-06-22 156 views
0

我使用的是使用mvc框架的asp.net 4。 当我更新一个值时,我的会话会丢失。 我似乎无法弄清楚为什么。我是ASP新手。更新值丢失会话变量

可以说我有Session["sValue"] (= "test")。 执行Index()后,该变量丢失

注:只发生,如果!string.IsNullOrEmpty(CallPrice)是真,那么CallPrice有一个值。

控制器:

// GET: Settings 
[Route("Settings")] 
public ActionResult Index() 
{ 
    SettingsModel pageSettings = new SettingsModel(); 
    string CallPrice = Request.QueryString[pageSettings.Query_CallPrice]; 
    ViewBag.Result = ""; 

    try 
    { 
     if (!string.IsNullOrEmpty(CallPrice)) 
     { 
      pageSettings.Price = Convert.ToInt32(CallPrice); 
      SettingsManager.call_price = CallPrice; 
      ViewBag.Result = "Update Succesful."; 
     } 
    } 
    catch (FormatException) 
    { 
     if (!string.IsNullOrEmpty(CallPrice)) 
      pageSettings.Price = Convert.ToInt32(SettingsManager.call_price); 

     ViewBag.Result = "Error Occured (Incorrect format provided)"; 
    } 

    return View(pageSettings); 
} 

页:

@model Actacom3CX.Models.SettingsModel 
@{ 
    ViewBag.Title = "3CX Settings"; 
} 
<head> 
    @{ 
     if (HttpContext.Current.Session[Actacom3CX.Classes.Statics.SESSION_USERNAME] == null) 
     { 
      Response.Redirect("~/UserLogon", false); 
     }else 
     { 
      HttpContext.Current.Session[Actacom3CX.Classes.Statics.SESSION_USERNAME] = HttpContext.Current.Session[Actacom3CX.Classes.Statics.SESSION_USERNAME]; 
     } 
    } 
</head> 
<div class="jumbotron" style="margin-top: 0em; padding-bottom:0.4em;padding-top:0.5em;"> 
    <div align="center"> 
     <h2 style="margin:0em;padding:0em;"> 
      @ViewBag.Title 
     </h2> 

     <h3 align="center"> 
      @{ 
       Output.Write(ViewBag.Result); 
      } 
     </h3> 

    </div> 
</div> 



<div class="row"> 
    <form id="settingsForm" method="get" action="~/Settings"> 
     <input class="btn btn-default" type="text" [email protected]{Output.Write(Model.Query_CallPrice);} [email protected]{Output.Write(Model.Price);} /><b style="padding-left: 1em;">Cent per minuut</b> 
     <br /><br /> 
     <input class="btn btn-default" type="submit" value="Update &raquo;" /> 
    </form> 
</div> 

UPDATE

会话迷失在下面的代码段正在发生,在设定管理:

public static void SetSetting(string key, string value) 
{ 
    Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); 
    configuration.AppSettings.Settings[key].Value = value; 
    configuration.Save(); 
    ConfigurationManager.RefreshSection("appSettings"); 
} 
+0

我觉得你的会话值永远不会改变,因为你是从自身分配吧! (在'else'之后的视图中) – alisabzevari

+0

已更新的代码,找到它发生的位置但不是原因 –

+0

错误的详细信息是什么? – alisabzevari

回答

1

感谢汉斯合成聚合物膜:

似乎要更新的web.config文件。当更新时,webapp重新启动,这会导致所有会话丢失(甚至来自其他用户!)。