我们在做asp.net的时候往往要取客户端的数据。一般的写法都是

  var q = Request.QueryString["xxx"];
            var f = Request.Form["xxx"];
            var c = Request.Cookies["xxx"].Value;
            var s = Request.ServerVariables["xxx"];

而我一般的写法是   var val = Request["xxx"];

虽然这种写法很简单但是问题也比较突出,如果QueryString、Form、Cookies、ServerVariables含有相同值得时候取那个值了?

public string this[string key]
{
    get
    {
        string str = this.QueryString[key];
        if (str != null)
        {
            return str;
        }
        str = this.Form[key];
        if (str != null)
        {
            return str;
        }
        HttpCookie cookie = this.Cookies[key];
        if (cookie != null)
        {
            return cookie.Value;
        }
        str = this.ServerVariables[key];
        if (str != null)
        {
            return str;
        }
        return null;
    }
}

要注意的地方时QueryString、Form、Cookies这3个都是客服端取到的,在asp.net4.0里面 都是做了验证的,验证的方法是RequestValidator.IsValidRequestString。根据具体情况可以重写该类的方法。

相关文章:

  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2022-01-06
猜你喜欢
  • 2022-02-14
  • 2022-01-15
  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案