Check if unassigned variable exists in Request.QueryString

后端 未结 5 722
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 05:59

Within the context of an ASP.NET page, I can use Request.QueryString to get a collection of the key/value pairs in the query string portion of the URI.

For example, if I

5条回答
  •  面向向阳花
    2021-02-07 06:15

    I wrote an extension method to solve this task:

    public static bool ContainsKey(this NameValueCollection collection, string key)
    {
        if (collection.AllKeys.Contains(key)) 
            return true;
    
         // ReSharper disable once AssignNullToNotNullAttribute
        var keysWithoutValues = collection.GetValues(null);
        return keysWithoutValues != null && keysWithoutValues.Contains(key);
    }
    

提交回复
热议问题