How to check that Request.QueryString has a specific value or not in ASP.NET?

前端 未结 8 1305
孤街浪徒
孤街浪徒 2020-12-08 06:12

I have an error.aspx page. If a user comes to that page then it will fetch the error path in page_load() method URL using Request.QueryStrin

8条回答
  •  旧巷少年郎
    2020-12-08 06:44

    Check for the value of the parameter:

    // .NET < 4.0
    if (string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"]))
    {
     // not there!
    }
    
    // .NET >= 4.0
    if (string.IsNullOrWhiteSpace(Request.QueryString["aspxerrorpath"]))
    {
     // not there!
    }
    

    If it does not exist, the value will be null, if it does exist, but has no value set it will be an empty string.

    I believe the above will suit your needs better than just a test for null, as an empty string is just as bad for your specific situation.

提交回复
热议问题