简体   繁体   中英

how do i update a querystring in asp.net mvc

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

Well, first off, that's not how you set default values. The values set in the constructor will only apply if the class is not initialized with different values. Your problem here is that the model binder will indeed initialize this class with null values, effectively negating what you're doing in the constructor.

The proper way to set a default value for a property is with a custom getter and setter:

private string culture;
public string Culture
{
    get { return culture ?? "En"; }
    set { culture = value; }
}

Or, if you can utilize C# 6+:

public string Culture { get; set; } = "En";

That code has the same effect as the previous, but is obviously much easier. Either way, the point here is that the property will return the default "En", whenever it's unset (ie null). Then, even if your class is initialized with null values, the default values will still be returned.

Additionally a line like the following is pointless:

mysession.HotelID = mysession.HotelID;

You're setting the variable to the current value of the variable, which makes no sense. It already has that value. Assuming you implement your default values properly as described above, you'll just get rid of all your else conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM