我如何更新asp.net mvc中的查询字符串

时间:2017-05-02 13:34:55

标签: c# asp.net-mvc asp.net-mvc-5 routing query-string

我在ASP.NET MVC中有一个控制器方法,如下所示:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.spark.deploy.worker.DriverWrapper$.main(DriverWrapper.scala:58)
    at org.apache.spark.deploy.worker.DriverWrapper.main(DriverWrapper.scala)
Caused by: java.io.IOException: Failed to open native connection to Cassandra at {127.0.1.1}:9042
    at com.datastax.spark.connector.cql.CassandraConnector$.com$datastax$spark$connector$cql$CassandraConnector$$createSession(CassandraConnector.scala:163)
    at com.datastax.spark.connector.cql.CassandraConnector$$anonfun$3.apply(CassandraConnector.scala:149)
    at com.datastax.spark.connector.cql.CassandraConnector$$anonfun$3.apply(CassandraConnector.scala:149)
    at com.datastax.spark.connector.cql.RefCountedCache.createNewValueAndKeys(RefCountedCache.scala:31)
    at com.datastax.spark.connector.cql.RefCountedCache.acquire(RefCountedCache.scala:56)
    at com.datastax.spark.connector.cql.CassandraConnector.openSession(CassandraConnector.scala:82)
    at com.datastax.spark.connector.cql.CassandraConnector.withSessionDo(CassandraConnector.scala:110)
    at com.datastax.spark.connector.cql.CassandraConnector.withClusterDo(CassandraConnector.scala:121)
    at com.datastax.spark.connector.cql.Schema$.fromCassandra(Schema.scala:322)
    at com.datastax.spark.connector.cql.Schema$.tableFromCassandra(Schema.scala:342)
    at com.datastax.spark.connector.rdd.CassandraTableRowReaderProvider$class.tableDef(CassandraTableRowReaderProvider.scala:50)
    at com.datastax.spark.connector.rdd.CassandraTableScanRDD.tableDef$lzycompute(CassandraTableScanRDD.scala:60)
    at com.datastax.spark.connector.rdd.CassandraTableScanRDD.tableDef(CassandraTableScanRDD.scala:60)
    at com.datastax.spark.connector.rdd.CassandraTableRowReaderProvider$class.verify(CassandraTableRowReaderProvider.scala:137)
    at com.datastax.spark.connector.rdd.CassandraTableScanRDD.verify(CassandraTableScanRDD.scala:60)
    at com.datastax.spark.connector.rdd.CassandraTableScanRDD.getPartitions(CassandraTableScanRDD.scala:232)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:248)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:246)
    at scala.Option.getOrElse(Option.scala:121)
    at org.apache.spark.rdd.RDD.partitions(RDD.scala:246)
    at org.apache.spark.rdd.RDD$$anonfun$distinct$2.apply(RDD.scala:401)
    at org.apache.spark.rdd.RDD$$anonfun$distinct$2.apply(RDD.scala:401)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:358)
    at org.apache.spark.rdd.RDD.distinct(RDD.scala:400)
    at com.access_company.twine.OnlineGatewayCount$.main(OnlineGatewayCount.scala:93)
    at com.access_company.twine.OnlineGatewayCount.main(OnlineGatewayCount.scala)
    ... 6 more
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.1.1:9042 (com.datastax.driver.core.exceptions.TransportException: [/127.0.1.1] Cannot connect))
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:233)
    at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
    at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1424)
    at com.datastax.driver.core.Cluster.getMetadata(Cluster.java:403)
    at com.datastax.spark.connector.cql.CassandraConnector$.com$datastax$spark$connector$cql$CassandraConnector$$createSession(CassandraConnector.scala:156)

此方法的路由如下所示:

   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);
      }
   }

我有session-helper类这个类我用来设置默认值并存储查询字符串参数的更改值,如果它们没有传递值:

     routes.MapRoute(
            "OpenCase",
            "IBE/hid={hid}",
            new { controller = "Home", action = "Index" }
        );

用户将使用以下网址:

 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

但是,我想更改查询字符串参数值,如果用户没有传递query-string参数的值,则查询字符串设置默认值例如,如果用户获取这样的url:  没有任何值的 http://localhost:51518/IBE/hid=100?Culture=Fr&Rate=ibe 我想从http://localhost:51518/IBE/hid=?Culture=&Rate= class

获取默认值

1 个答案:

答案 0 :(得分:1)

嗯,首先,这不是你如何设置默认值。只有在未使用不同的值初始化类时,构造函数中设置的值才会适用。你的问题是模型绑定器确实会用null值初始化这个类,这实际上否定了你在构造函数中所做的事情。

为属性设置默认值的正确方法是使用自定义getter和setter:

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

或者,如果您可以使用C#6 +:

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

该代码与前一代码具有相同的效果,但显然更容易。无论哪种方式,这里的要点是该属性将返回默认值" En",只要它未设置(即为空)。然后,即使您的类使用空值初始化,仍将返回默认值。

此外,如下所示的行是没有意义的:

mysession.HotelID = mysession.HotelID;

您将变量设置为变量的当前值,这没有任何意义。它已经具有该值。假设您如上所述正确实施了默认值,那么您就可以摆脱所有else条件。