C# 为什么我们使用HttpContext.Current?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10714189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:56:46  来源:igfitidea点击:

why do we use HttpContext.Current?

c#asp.net

提问by Masoud Darvishian

I don't know, why do we use HttpContext.Current?
In this property I use it for Sessionbut I don't know why!

我不知道,我们为什么要使用HttpContext.Current
在这个属性中我使用它,Session但我不知道为什么!

public static string Name
{
    get
    {
         if (HttpContext.Current.Session["_n_"] != null)
            return HttpContext.Current.Session["_n_"].ToString();
         else return "";
    }
    set
    {
         HttpContext.Current.Session["_n_"] = value;
    }
}

采纳答案by Oybek

HttpContextis an object that wraps all http related information into one place. HttpContext.Currentis a context that has been created during the active request. Here is the list of some data that you can obtain from it.

HttpContext是一个将所有 http 相关信息包装到一个地方的对象。HttpContext.Current是在活动请求期间创建的上下文。以下是您可以从中获取的一些数据的列表。

  1. Request type (Post, Get)
  2. Request parameters (querystring, posted data)
  3. User's IP address
  4. Cookies
  1. 请求类型(发布、获取)
  2. 请求参数(查询字符串、发布的数据)
  3. 用户的 IP 地址
  4. 饼干

Further you can control your output through this object. In Itemsproperty, which is a dictionary, you can store instances of objects to ensure that they are created once for the request. You can control the output stream applying your custom filters.

此外,您可以通过此对象控制您的输出。在Items属性中,它是一个字典,您可以存储对象的实例以确保它们为请求创建一次。您可以控制应用自定义过滤器的输出流。

This is a short list of that what you can do with this property.

这是您可以使用此属性执行的操作的简短列表。

回答by Darren Kopp

It's a way to get access to the current HttpContext someplace that may not have a reference to the context but is within an active web request.

这是一种访问当前 HttpContext 的方法,该位置可能没有对上下文的引用,但在活动的 Web 请求中。

回答by Erik Funkenbusch

That's like saying "Why do I need to go to a bank to get money?", to which the answer is "Because that's where the money is.

这就像说“为什么我需要去银行取钱?”,答案是“因为钱就在那里。

To answer your question. Because that's where the Session is. It's really that simple. You don't have to know why, just that that's where it is.

回答你的问题。因为那是 Session 所在的地方。真的就是这么简单。你不必知道为什么,这就是它的所在。

There's a much longer explanation, which other people are giving with all the technical details. But in the end, the answer just boils down to this.

有一个更长的解释,其他人提供了所有技术细节。但最终,答案只能归结为这个。

回答by Mohamed Fathallah

before asp.net MVC in a web form, there were classes request, response where you can get cookies and session and those staff in MVC all the HTTP information like request and response and their properties are now inside HTTpcontext.

在 web 表单中的 asp.net MVC 之前,有类请求、响应,您可以在其中获取 cookie 和会话,MVC 中的那些工作人员所有的 HTTP 信息(如请求和响应)及其属性现在都在 HTTpcontext 中。