在.NET/C#上有一种isset()吗?

mar*_*zzz 13 .net c#

我需要检查uri请求是否有一个参数.

所以我需要检查是否设置了该参数..NET/C#上有功能吗?

jru*_*ell 22

未设置的值将为null或空:

value = Request["param"];
if (String.IsNullOrEmpty(value))
{
    // param is not set
}
Run Code Online (Sandbox Code Playgroud)

如果参数名称在查询字符串中但值不是,则它将为空.如果名称不在查询字符串中,则该值将为null.例如.

&param1=&param2=value 
// Request["param1"] is String.Empty
// Request["param3"] is null
Run Code Online (Sandbox Code Playgroud)

  • param1设置为空字符串.是否应该将其视为与未设置相同是依赖于需求的,但它仍然是设置的. (3认同)