【问题标题】:Null in context.Request上下文中为空。请求
【发布时间】:2012-10-11 06:23:03
【问题描述】:

如何在Context.Request中允许null:

context.Response.Write(retrieveList(context.Request["SalCode"].ToString(null), context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));

【问题讨论】:

    标签: c# null httpcontext


    【解决方案1】:

    首先,Request["..."] 已经返回了一个string,因此无需在其上调用ToString(),因此在此阶段无需担心它是否返回null(即,如果密钥是请求中不存在)。

    因此你可以调用例如

    retrieveList(
        context.Request["SalCode"],
        context.Request["groupKeyword"],
        context.Request["text"]);
    

    不用担心这三个中的任何一个是否为空。

    然后,如果任何输入为空,您可以更改 retrieveList 以正确响应。例如,您可以返回 null:

    private string /*or whatever*/ retrieveList(
        string salCode, string groupKeyword, string text)
    {
        if (String.IsNullOrEmpty(salCode) ||
            String.IsNullOrEmpty(groupKeyword) ||
            String.IsNullOrEmpty(text))
        {
            return null;
        }
        ...
    }
    

    然后,注意Response.Write 不在乎你是否给它一个null,它只是什么都不写,所以你可以像上面一样继续调用Write

    或者,您可以检查返回值,如果是null,则写一条消息:

    var list = retrieveList(
        context.Request["SalCode"],
        context.Request["groupKeyword"],
        context.Request["text"]));
    if (!String.IsNullOrEmpty(list))
    {
        context.Response.Write(list);
    }
    else
    {
        context.Response.Write("Missing request parameter.");
    }
    

    【讨论】:

      【解决方案2】:

      你没有指定任何东西!请添加信息,您甚至没有指定发生此问题的环境、类或一般上下文。我们也不知道retrieveList() 的签名是什么!这让我们很难为您提供帮助!你有多想回答这个问题,我的一位同事曾经面临过:“这个废话行不通!” ? (是的,这甚至不是一个问题,而是发生在现实生活中的支持情境中!)

      我注意到的一件事是您使用 *T*oString() 而不是 *t*oString(),我认为这是一个错字。 (已编辑,因为很明显这是 C#)另外,我不知道 .toString(null) 是什么意思。您想告诉我们该语句导致 NullPointerException 吗?在这种情况下,您本可以为我们付出更多努力来理解您的问题,例如把它写下来……

      顺便说一句,如果是这样的话,我会说,这将解决你的问题:

      Object salCode = context.Request["SalCode"];
      context.Response.Write(retrieveList(salCode==null?"":salCode.ToString(), context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));
      

      编辑我认为(但无法测试)如果 null 是问题,这将解决它。但是,如果底层代码在指定空字符串的情况下无法正常工作,则应该像这样在retrieveList() 中检查(粘贴自引用的帖子):

      private string retrieveList(string SalCode, string groupKeyword, string text)
      {
          SqlConnection _sqlCon = default(SqlConnection);
          SqlCommand _sqlCom = default(SqlCommand);
          SqlDataReader _sqlReader = default(SqlDataReader);
          StringBuilder _sb = new StringBuilder();
          List<TokenInputJSON> _out = null;
      
          try
          {
              _sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SalesianBlastConnectionString"].ConnectionString);
              _sqlCom = new SqlCommand("getTokenInput", _sqlCon);
              _sqlCom.CommandType = CommandType.StoredProcedure;
              _sqlCon.Open();
      
              //This is the edited part
              if(SalCode==null || SalCode.Equals("")) {
                  _sqlCom.Parameters.Add(new SqlParameter("@salCode", SqlDbType.VarChar, 4)).Value = SalCode;
              }
              //... continue with the other parts
      

      快速检查:我刚刚有了一个想法:在调用retrieveList时使用一个常量值来确定是否是问题所在:

      context.Response.Write(retrieveList("enterAValidSalCodeHere", context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));
      

      【讨论】:

      • 这是我的整个代码所在,我的问题在 C# (stackoverflow.com/questions/12832385/…)
      • 糟糕,我误以为是 Java。我将尝试增强我的答案以使其正确-尽管很长时间以来我都没有在C#中编程...但是,问题在于SalCode,它在retrieveList()中用作查询的参数, 一片空白。这是一个有效的工作条件吗?当 SalCode 不包含有效值时,您的程序应该如何工作?
      • 您也可以像这样使用空合并运算符 (??):salCode.ToString() ?? "" - 基本上翻译为“如果 salCode.ToString() 为空,请使用 "" ";虽然因为你没有做作业,所以这可能行不通。
      • 编辑了我的答案,请检查是否有效。遗憾的是,我无法对其进行测试,因为我无法在附近的任何地方访问 Visual Studio...
      • @Tim 我试过了,但错误提示“对象引用未设置为对象的实例。”
      猜你喜欢
      • 2018-06-16
      • 2017-08-21
      • 2019-06-26
      • 2021-06-19
      • 2020-12-07
      • 2016-06-01
      • 2014-05-06
      • 2018-01-06
      • 2019-04-12
      相关资源
      最近更新 更多