English 中文(简体)
Request["key"] vs Request.Params["key"] vs Request.QueryString["key"]
原标题:

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

Which method do you seasoned programmers use? and why?

最佳回答

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.

问题回答

I prefer to use Request.QueryString["key"] because it helps the code-reader know exactly where you are getting the data from. I tend not to use Request.Params["key"] because it could refer to a cookie, query string and a few other things; so the user has to think a little. The less time someone needs to figure out what you are thinking, the easier it is to maintain the code.

HttpRequest.Params or Request.Params gets just about everything (querystring, form, cookie and session variables) from the httprequest, whereas Request.Querystring only will pull the querystring... all depends on what you are doing at the time.

I always explicitly specify the collection. If for some reason you want to allow overrides, code the "get" for each one and write some clear code that shows your hierarchy for choosing one over the other. IMO, I dislike getting a value from multiple sources without a clear business reason for so doing.

As a kindly notice, If you set requestValidationMode="4.5" under web.config, both Request.QueryString[“key”] and Request[“key”] will use "lazy loading" behavior as design.

However, somehow Request.Params[“key”] will still trigger validation as the behavior of 4.0 .

This odd behavior really confuses me for a long time.

Always found it more useful to target url params with the request.querystring, it saves the headache of trying to track down other values it can grab from various other locations.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签