I have a controller method in ASP.NET MVC that looks like this:
public class HomeController : Controller
{
SessionHelper mysession = new SessionHelper();
[HttpGet]
[ActionName("Index")]
public ActionResult Index_Get(SessionHelper arg)
{
pageload();
if (Request["hid"] != null)
{
mysession.HotelID = Request["hid"];
}
else
{
mysession.HotelID = mysession.HotelID;
//if HotelID==null i want to get the value to querystring i do it like this but did not worked
}
if (Request["Culture"] != null)
{
mysession.Culture = Request["Culture"];
}
else
{
mysession.Culture = mysession.Culture;
//if Culture ==null i want to get the value to querystring i do it like this but did not worked
}
return View(mysession);
}
}
The routing for this method looks like this:
routes.MapRoute(
"OpenCase",
"IBE/hid={hid}",
new { controller = "Home", action = "Index" }
);
I have session-helper class this class i used to set default values and store the changed values for the query string parameters if their is no value passed :
public class SessionHelper
{
public SessionHelper()
{
// ------ Set default values here
Rate = "";
HotelID = string.Empty;
CSS_FileName = "default.css";
Culture = "En";
Checkin = DateTime.Today.Date;
Checkout = DateTime.Today.Date.AddDays(1);
//Maximum numbers
MaximumNumberOfRooms = 4;
MaximumNumberOfAdultsPerRoom = 4;
MaximumNumberOfChildrenPerRoom = 3;
MaximumDaysAheadBookable = 450;
MaximumDaysBetweenCheckinCheckout = 31;
}
#region String properties
public string Rate
{
get { return HttpContext.Current.Session["Rate"] as string; }
set { HttpContext.Current.Session["Rate"] = value; }
}
public string CSS_FileName
{
get { return HttpContext.Current.Session["CSS_FileName"] as string; }
set { HttpContext.Current.Session["CSS_FileName"] = value; }
}
public string Culture
{
get { return HttpContext.Current.Session["Culture"] as string; }
set { HttpContext.Current.Session["Culture"] = value; }
}
#endregion
#region Integer properties
public string HotelID
{
get { return (string)(HttpContext.Current.Session["HotelID"]); }
set { HttpContext.Current.Session["HotelID"] = value; }
}
//Maximum numbers defaulot value
public int MaximumNumberOfRooms
{
get { return (int)(HttpContext.Current.Session["MaximumNumberOfRooms"]); }
set { HttpContext.Current.Session["MaximumNumberOfRooms"] = value; }
}
public int MaximumNumberOfAdultsPerRoom
{
get { return (int)(HttpContext.Current.Session["MaximumNumberOfAdultsPerRoom"]); }
set { HttpContext.Current.Session["MaximumNumberOfAdultsPerRoom"] = value; }
}
public int MaximumNumberOfChildrenPerRoom
{
get { return (int)(HttpContext.Current.Session["MaximumNumberOfChildrenPerRoom"]); }
set { HttpContext.Current.Session["MaximumNumberOfChildrenPerRoom"] = value; }
}
public int MaximumDaysAheadBookable
{
get { return (int)(HttpContext.Current.Session["MaximumDaysAheadBookable"]); }
set { HttpContext.Current.Session["MaximumDaysAheadBookable"] = value; }
}
public int MaximumDaysBetweenCheckinCheckout
{
get { return (int)(HttpContext.Current.Session["MaximumDaysBetweenCheckinCheckout"]); }
set { HttpContext.Current.Session["MaximumDaysBetweenCheckinCheckout"] = value; }
}
#endregion
#region Date properties
public DateTime Checkin
{
get { return (DateTime)(HttpContext.Current.Session["Checkin"]); }
set { HttpContext.Current.Session["Checkin"] = value; }
}
public DateTime Checkout
{
get { return (DateTime)(HttpContext.Current.Session["Checkout"]); }
set { HttpContext.Current.Session["Checkout"] = value; }
}
#endregion
The user will use the following URL :
http://localhost:51518/IBE/hid=100?Culture=Fr&Rate=ibe
However, I'd like to change the query-string parameter values if the user didn't pass values for query-string parameter the query-string set the default values fro example if the user get the url like this :
http://localhost:51518/IBE/hid=?Culture=&Rate=without any values i want to get the default values from mysession class