【问题标题】:How do I check for a segment in query string [duplicate]如何检查查询字符串中的段[重复]
【发布时间】:2015-11-10 14:55:59
【问题描述】:

我收到以下错误,但由于某种原因,它在 uri 中不可用的 guid 上,我想我可以检查一下是否为空,但当然密钥不存在

这不是重复它询问如何检查它自己的实际密钥以查看它是否存在。 !!!

Line 23: 
Line 24:                 player _player = new player();
Line 25:                 Guid id = new         
Guid(Request.QueryString["id"].ToString());
Line 26:                 if (id == Guid.Empty)
Line 27:                 {

这是整个代码

protected void Page_Load(object sender, EventArgs e)
    {

        try
        {

            player _player = new player();
            Guid id = new Guid(Request.QueryString["id"].ToString());
            if (id == Guid.Empty)
            {


            }
            else
            {
                _player = _dal.GetPlayerBYID(id);


                if (!Page.IsPostBack)
                {
                    if (_player.Name != null)
                        txtFullName.Text = _player.Name;
                    if (_player.address != null)
                        txtAddress.Text = _player.address;

                    dlGenders.SelectedValue = _player.gender;

                }


            }


            var dlGendersSource = _dal.GetGenders();
            dlGenders.DataSource = dlGendersSource;
            dlGenders.DataValueField = "LookupValue";
            dlGenders.DataTextField = "LookupDescription";
            dlGenders.DataBind();



            string message = "";
            if (Context.User.IsInRole("canEdit"))
            {
                //enable edit controls on page

                message += "This user can edit a record";
            }
            else if (Context.User.IsInRole("canDelete"))
            {
                //enable delete controls on page
                message += "This user can delete a record.";
            }
            else if (Context.User.IsInRole("canAdd"))
            {
                //enable delete controls on page
                message += "This user can add a record.";
            }
            RadNotification1.Position = NotificationPosition.BottomRight;
            RadNotification1.Text = message;


        }
        catch (Exception ex)
        {
            throw new EntityContextException("Page Load Failed in Edit Players  .", ex);
        }


    }

显示错误的屏幕截图这会导致团队一开始并不总是附加玩家 ID,因此我需要能够处理该问题,直到调用保存更改。

伙计们,这是我遇到问题的部分,所以没有反对意见!!!!

       Guid id = new Guid(Request.QueryString["id"]);

【问题讨论】:

  • 你遇到了什么错误?
  • Request.QueryString["id"] 已经是一个字符串,如果它的 null 调用 ToString() 是个坏主意。
  • 它是一个空错误,所以它是但我不会仍然得到空值
  • string id = Request.QueryString["id"]; if (!string.IsNullOrEmpty(id)) { ... use it ...}
  • g 是 Guid() 构造函数参数内部名称,您将 null 作为其值传递,这是无效的,因此会出现错误。

标签: c# asp.net


【解决方案1】:

改成这样

        Guid id = new Guid(Request.QueryString["id"] ?? "");
        if (id == Guid.Empty)

...

【讨论】:

  • 感谢您的积极参与
  • 那会抛出; new Guid("") 不比 new Guid(null) 更有效
  • 它仍然在 System.FormatException 中显示 guid:无法识别的 Guid 格式。
  • 你必须传递一个有效的 guid string,如果你的 id 值不存在,你就没有,你想做什么?
  • 如果没有 id 你想要一个新的 Guid 吗? -> string id = Request.QueryString["id"]; var guid = string.IsNullOrEmpty(id) ? new Guid() : new Guid(id);
猜你喜欢
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
相关资源
最近更新 更多