【问题标题】:Get Guid with query string使用查询字符串获取 Guid
【发布时间】:2014-06-11 08:17:30
【问题描述】:

我在这样的变量中得到一个 Guid 值

var getvalueGuid = db.Clients.Where(u => u.Numero_telephone == 
TextBox_numero_telephone.Text).Select(u => u.GuID).FirstOrDefault();

我想把它转换成这样的查询字符串:

getvalueGuid = Request.QueryString["id"];

怎么办?

【问题讨论】:

    标签: asp.net guid


    【解决方案1】:

    你可以使用Guid.TryParse:

    Guid getvalueGuid;
    if(Guid.TryParse(Request.QueryString["id"], out getvalueGuid))
    {
        // successfully parsed 
    }
    

    【讨论】:

    • 事实上,我需要像这样在邮件正文中传递它: string body = "

      " + "Bonjour," + "

      " + "vous venez de modifier vos notifications电子邮件倾诉逻辑:“+ DropDownList_choixlogiciel.SelectedValue +“

      ”+“Vous avez choisi de recevoir les newsletters relatedant le(s) groupe(s):”+“
      -”+stringAbonnements+“" + "

      " + "Veuillez Confirmer vos choix :" + getvalueGuid + "

      " + "Cordialement, L'équipe Septeo .";

    • @user3711519:有什么问题? getvalueGuid 在 if 中初始化。
    • 我做那个 var getvalue = db.Clients.Where(u => u.Numero_telephone == TextBox_numero_telephone.Text).Select(u => u.GuID).FirstOrDefault();指导 getvalueGuid; if (Guid.TryParse(Request.QueryString["id"], out getvalueGuid)) { getvalue = getvalueGuid; }
    • @user3711519:“它不起作用”是什么意思?为什么选择一个 guid,只是为了在下一行分配一个不同的 guid?我不明白你想要实现什么,什么没有奏效。为什么需要getvalue = getvalueGuid;?
    • 我想在正文中获取 Guid 变量,然后,当用户单击链接(guid)时,将出现一个新的 aspx 页面
    【解决方案2】:

    您将能够在 QueryString 中获取它,前提是您拥有类似的网址

    www.example.com/page?id=[guid_here]

    然后,当您使用该代码时,它会为您提供一个字符串,该字符串将包含 URL 中提供的查询字符串。

    【讨论】:

      【解决方案3】:

      由于缺少很多细节,很难理解您的问题,但我认为您想从查询字符串中获取强类型 GUID 值?

      System.Guid 没有TryParse 方法,因此您必须使用构造函数并捕获任何抛出的异常:

      如果是这样,那么这样做:

      String guidStr = Request.QueryString["id"];
      Guid guid = null;
      try {
          guid = new Guid( guidStr );
      } catch(ArgumentNullException) {
      } catch(FormatException) {
      } catch(OverflowException) {
      }
      
      if( guid == null { 
          // Inform user that the GUID specified was not valid.
      }
      

      三个例外(ArgumentNullException、FormatException 和 OverflowException 记录在 Guid(String) 构造函数的注释中:http://msdn.microsoft.com/en-us/library/96ff78dc%28v=vs.110%29.aspx

      更新:

      我忘记了 .NET 4.0 引入了 TryParse 方法。如果您使用的是 .NET 4.0 或更高版本,请改用它:http://msdn.microsoft.com/en-us/library/system.guid.tryparse%28v=vs.110%29.aspx

      【讨论】:

        【解决方案4】:
        Guid requestGuid;
        if (Guid.TryParse(Request.QueryString["id"], out requestGuid))
        {
           // Do logic here with requestGuid
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-12-25
          • 1970-01-01
          • 2011-03-12
          • 1970-01-01
          • 2016-12-08
          • 2019-03-10
          • 2020-04-05
          相关资源
          最近更新 更多