[英]what HttpContext.Current.Request[“CarName”] != null, what it is doing?
I am modifying a subject line of code but did not get it. 我正在修改代码的主题行,但没有得到。 I was thinking HttpContext.Current.Request
is read only and it return the url of request. 我在想HttpContext.Current.Request
是只读的,它返回请求的url。 Can we set value for HttpContext.Current.Request["CarName"]
just like a variable or a session variable? 我们可以像变量或会话变量一样为HttpContext.Current.Request["CarName"]
设置值吗?
Please guide me what this line is doing. 请指导我这行在做什么。
Edit: 编辑:
if (HttpContext.Current.Request["CarName"] != null){
}
The indexer method on the Request
object looks up the KeyValuePair
's within Request
's members. Request
对象上的索引器方法在Request
的成员中查找KeyValuePair
。 It is important to note that Request
is an entire object.. not just a URL. 重要的是要注意Request
是一个完整的对象..不仅仅是URL。
ILSpy shows this as the implementation of the indexer method: ILSpy将其显示为indexer方法的实现:
public string this[string key]
{
get
{
string text = this.QueryString[key];
if (text != null)
{
return text;
}
text = this.Form[key];
if (text != null)
{
return text;
}
HttpCookie httpCookie = this.Cookies[key];
if (httpCookie != null)
{
return httpCookie.Value;
}
text = this.ServerVariables[key];
if (text != null)
{
return text;
}
return null;
}
}
So, your line of code is checking to see if "CarName" is a key contained in any of the above KeyValuePair
members of the Request
object. 因此,您的代码行将检查“ CarName”是否为Request
对象的上述任何KeyValuePair
成员中包含的键。
See HttpContext.Request.Item (When looking for x[]
syntax, lookup x.Item
): 请参见HttpContext.Request.Item (在查找x[]
语法时,查找x.Item
):
Gets the specified object from the QueryString , Form , Cookies , or ServerVariables collections. 从QueryString , Form , Cookies或ServerVariables集合中获取指定的对象。
If a value with the given key is not found from one of the sources then null
is returned. 如果从任一来源中未找到具有给定键的值,则返回null
。 In this case, "CarName" is presumably supplied in a valid request (eg ../search?CarName=Rusty
) so the conditional might check for "no search conditions". 在这种情况下,“ CarName”大概是在有效请求中提供的(例如../search?CarName=Rusty
),因此条件可以检查“无搜索条件”。
This is not the same as using sessions! 这是不一样的使用会话! Sessions provide a generic mechanism to associated data (but can be backed by direct cookies) "per session". 会话为“每个会话”关联数据提供了一种通用机制(但可以由直接cookie支持)。 In any case, never directly trust user data obtained from the client as it can be spoofed. 无论如何,永远不要直接信任从客户端获得的用户数据,因为它们可能是被欺骗的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.