HttpContext, HttpRequest, HttpResponse Common Object Summary

Source: Internet
Author: User
Tags httpcontext urlencode

Recently Old and HttpContext, HttpRequest, HttpResponse HttpRuntime, httpserverutility these objects to deal with. It's time to put them down.

A HttpRuntime exposes a static method UnloadAppDomain (), which allows us to restart the site with code. It is usually used for user to modify a more important parameter through the program interface, then need to restart the program.

HttpRuntime also exposes a Cache of static properties that everyone knows well. Some people may think that he/she is using Page.cache or httpcontext.cache, in fact, the latter two attributes are Httpruntime.cache "shortcuts".

An instance of HttpServerUtility is usually exposed as a property of the server, but it provides some encode, decode method actually calls the static method of the HttpUtility class. HttpUtility exposes a number of static methods, such as:
HtmlEncode () should be used to prevent injection attacks by using a higher frequency method, which is responsible for safely generating a section of HTML code.
Sometimes we also need to generate a URL, then the UrlEncode () method can be used, because the URL does not contain all the characters, so to do the appropriate encoding.
HttpUtility also has a method Htmlattributeencode (), which is also used to prevent injection attacks and to safely output an HTML attribute.
In. Net4, HttpUtility also provides another method: Javascriptstringencode (), and also to prevent injection attacks, securely output a section of JS code on the server. HttpUtility also exposes a number of static methods, such as:
HtmlDecode (), UrlDecode (), generally, we do not need to use them. Especially UrlDecode, unless you want your own framework, in general, when we visit the QueryString, form, has done urldecode, you do not have to call again. In addition to exposing the more commonly used encode, decode methods, HttpServerUtility also exposes a very useful method: Execute (), yes, it is very useful, especially if you need to get a page or user control HTML output on the server side.

Two HttpRequest: enables asp.net to read HTTP values that the client sends during a WEB request. The HttpRequest instance contains all the data from the client, which we can think of as input data, handler and module as the process, HttpResponse is output.

Of all the input data contained in HttpRequest, there are QueryString, Form, cookies that we often use, and it also allows us to access some HTTP request headers, browser-related information, request mapping related file paths, URL details, request methods, Whether the request has been authenticated, is SSL, and so on.

Most of the public properties of HttpRequest are more important//Get the virtual application root path of the ASP.net application on the server.

public string Applicationpath {get;}
Gets the virtual path of the application root and makes the path a relative path by using the tilde (~) notation for the application root (for example, in the form of "~/page.aspx").

public string Apprelativecurrentexecutionfilepath {get;}
Gets or sets information about the browser capabilities of the client being requested.

Public HttpBrowserCapabilities Browser {get; set;}
Gets a collection of cookies sent by the client.

Public HttpCookieCollection cookie {get;}
Gets the virtual path of the current request.

public string FilePath {get;}
Gets a collection of files uploaded by the client in multipart MIME format.

Public httpfilecollection Files {get;}
Gets or sets the filter to use when reading the current input stream.

Public Stream Filter {get; set;}
Gets a collection of form variables.

Public NameValueCollection Form {get;}
Gets the HTTP header collection.

Public NameValueCollection Headers {get;}
Gets the HTTP data transfer method used by the client (such as GET, POST, or head).

public string HttpMethod {get;}
Gets the contents of the incoming HTTP entity body.

Public Stream InputStream {get;}
Gets a value that indicates whether the request was validated.

public bool IsAuthenticated {get;}
Gets the virtual path of the current request.

public string Path {get;}
Gets the collection of HTTP query string variables.

Public NameValueCollection querystring {get;}
Gets the original URL of the current request.

public string Rawurl {get;} Get information about the current pleaseThe information for the URL to be asked.

Public Uri Url {get;}
Gets the specified object from the QueryString, Form, Cookie, or ServerVariables collection.

public string this[string key] {get;}
Maps the specified virtual path to a physical path.
Parameters: VirtualPath: The virtual path (absolute or relative) of the current request.
Return Result: The server physical path specified by virtualpath. public string MapPath (string virtualpath);

HttpRequest's QueryString, the type of the form property is NameValueCollection "is a case-insensitive collection", and its collection type has a feature that allows multiple string values to be stored under one key. Such as:

Response.Redirect ("? aa=1&bb=2&cc=3&aa=" + Httputility.urlencode ("5,6,7"), true);

String AA=REQUEST.QUERYSTRING[AA];

The value of AA is: "1,5,6,7"

HttpRequest also has two property Params,item for easy access to HTTP data, which allows us to write code without distinguishing between get or post, and the only difference between these two properties is that the Item accesses the 4 sets sequentially, and finds the result, params, when accessed, merges data from 4 collections into a new collection (created when the collection does not exist), and then finds the specified result.

three HttpResponse: Returns the result to the client. All operations that need to be returned to the client call the HttpResponse method. It provides functionality focused on the operation of the HTTP response section, such as response flow, response header. Gets the caching policy (Expiration time, privacy, change clause) of the Web page.

Public HttpCachePolicy Cache {get;}

Gets or sets the HTTP MIME type of the output stream. The default value is "text/html". Public
string ContentType {get; set;}

Gets a collection of response cookies. Public
httpcookiecollection Cookie {get;}

Gets or sets a wrapper filter object that is used to modify the HTTP entity body before transmission. Public
Stream Filter {get; set;}

Enables binary output to the output Http content body. Public
Stream OutputStream {get;}

Gets or sets the HTTP status code of the output returned to the client. The default value is (OK). public
int StatusCode {get; set;}

Adds an HTTP header to the output stream. Public
void Appendheader (string name, string value);

Sends all current buffered output to the client, stops execution of the page, and raises the EndRequest event. Public
void End ();

Redirects the client to the new URL. Specifies the new URL and specifies whether execution of the current page should terminate. Public
void Redirect (string URL, bool endresponse);

Writes the specified file directly to the HTTP response output stream, rather than buffering the file in memory. Public
void TransmitFile (string filename);

Writes System.Object to the HTTP response stream. Public
void Write (object obj);

four HttpContext"can be found anywhere" –> derived HttpRequest, HttpResponse, HttpServerUtility, Cache properties User, Items, current

User: stored in the current request identity information, if the current request is determined that the user is not authenticated, you can access: request.isauthenticated this instance property.

Items:datatype dictionary, so it is suitable to be accessed in a key/value way, and data will not be shared among different requests.

Current: HttpContext is the key to using several of its main derivations (HttpRequest, HttpResponse, Server, Cache) anywhere


In addition, many properties are not in the WEBAPI. Here's how to get the post data and I'll take a look at the method. Usually automatically turns into objects that are used in the action.

[HttpPost]
public void Confirmation (Httprequestmessage request)
{
    var content = Request. Content;
    String jsoncontent = content. Readasstringasync (). result;
}
Where the Request object is Httpcontrollercontext.request



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.