Request[“key”] vs Request.Params[“key”] vs Request.QueryString[“key”]

前端 未结 5 1900
攒了一身酷
攒了一身酷 2020-12-07 14:04

Request[\"key\"] vs Request.Params[\"key\"] vs Request.QueryString[\"key\"]

Which method do you seasoned programmers use? and

5条回答
  •  死守一世寂寞
    2020-12-07 15:01

    I recommend Request.QueryString["key"]. There isn't a lot of difference to Request["Key"] for a query string but there is a big(er) difference if you are trying to get the value from ServerVariables. Request["Key"] looks for a value in QueryString if null, it looks at Form, then Cookie and finally ServerVariables.

    Using Params is the most costly. The first request to params creates a new NameValueCollection and adds each of the QueryString, Form, Cookie and ServerVariables to this collection. For the second request on it is more performant than Request["Key"].

    Having said that the performance difference for a couple of keys is fairly negligable. The key here is code should show intent and using Request.QueryString makes it clear what your intent is.

提交回复
热议问题