Reputation: 2913
I am using asp .net 4 using mvc framework. When I update a value, my session gets lost. I cannot seem to figure out why. I am new to ASP.
Lets say I have Session["sValue"] (= "test")
.
After executing the Index()
, that variable is lost
Note: It only happens if
!string.IsNullOrEmpty(CallPrice)
is True, so CallPrice has a value.
The Controller:
// 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);
}
The Page:
@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" name=@{Output.Write(Model.Query_CallPrice);} value=@{Output.Write(Model.Price);} /><b style="padding-left: 1em;">Cent per minuut</b>
<br /><br />
<input class="btn btn-default" type="submit" value="Update »" />
</form>
</div>
UPDATE
The Session getting lost is happening in the following piece of code, in the SettingsManager:
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");
}
Upvotes: 0
Views: 953
Reputation: 2913
Thanks to Hans Kesting:
It seems you are updating the web.config file. When that is updated, the webapp restarts, which causes all Sessions to get lost (even from other users!).
Upvotes: 1