简体   繁体   English

什么HttpContext.Current.Request [“ CarName”]!= null,它在做什么?

[英]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. QueryStringFormCookiesServerVariables集合中获取指定的对象。

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.

相关问题 生产中的HttpContext.Current.Request为null - HttpContext.Current.Request null in production HttpContext.Current.Items []和HttpContext.Current.Request []之间有什么区别? - What's the differences between HttpContext.Current.Items[] and HttpContext.Current.Request[]? 在HttpContext.Current.Request中模拟ServerVariables - Mock ServerVariables in the HttpContext.Current.Request HttpContext.Request或HttpContext.Current.Request URL中是否缺少“#”字母? - '#' letter is missing in HttpContext.Request or HttpContext.Current.Request url? HttpContext.Current.Request在外部C#类中导致空对象引用 - HttpContext.Current.Request resulting in null object reference in Outer C# class 我可以使用动态重定向HttpContext.Current.Request吗? - can I use dynamic to redirect HttpContext.Current.Request? HttpControllerContext.Request和HttpContext.Current.Request之间的区别 - Difference between HttpControllerContext.Request and HttpContext.Current.Request HttpContext.Current.Request和Page.Request中的Url.Host - Url.Host in HttpContext.Current.Request and Page.Request 带有Angular UI路由器的HttpContext.Current.Request - HttpContext.Current.Request with Angular ui-router 我想在我的Windows窗体中包含HttpContext.Current.Request - I want to include HttpContext.Current.Request in my windows forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM