Converting/accessing QueryString values in ASP.NET

后端 未结 7 1743
长情又很酷
长情又很酷 2021-01-30 14:44

I\'m curious what everyone does for handling/abstracting the QueryString in ASP.NET. In some of our web apps I see a lot of this all over the site:

int val = 0;         


        
7条回答
  •  名媛妹妹
    2021-01-30 15:18

    One thing is you're not capturing blank values here. You might have a url like "http://example.com?someKey=&anotherKey=12345" and in this case the "someKey" param value is "" (empty). You can use string.IsNullOrEmpty() to check for both null and empty states.

    I'd also change "someKey" to be stored in a variable. That way you're not repeating literal strings in multiple places. It makes this easier to maintain.

    int val = 0;
    string myKey = "someKey";
    if (!string.IsNullOrEmpty(Request.QueryString[myKey]))
    {
        val = int.Parse(Request.QueryString[myKey]);
    }
    

    I hope that helps!

    Ian

提交回复
热议问题