. NET three core objects

Source: Internet
Author: User
Tags allkeys

. NET three core objects HttpRequest

Now it's time for the first core object to appear. MSDN gave it a brief explanation: "Enables ASP. To read HTTP values that the client sends during a WEB request. ”
This explanation is still in place. HttpRequest instance contains all the data from the client , we can think of this data as input data, handler and module is equivalent to the processing process, HttpResponse is the output up.

Among all the input data contained in HttpRequest, there are QueryString, Form, Cookie, which we often use,

It also allows us to access some HTTP request headers, information about the browser, the associated file path of the request map, the URL details, the requested method, whether the request has been authenticated, whether it is SSL, and so on.

Most of the public properties of HttpRequest are more important, so here's a simple list of them.

Gets 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 the collection of cookies sent by the client. Public httpcookiecollection Cookies {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 the form variable collection. 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 principal. 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 a collection of HTTP query string variables. Public NameValueCollection QueryString {get;} Gets the original URL of the current request. public string RawUrl {get;} Gets information about the URL of the current request.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. Parameter: virtualpath: The virtual path of the current request (absolute path or relative path). Return Result: The physical path of the server specified by VirtualPath. public string MapPath (string virtualpath);

  

Let me say some details that are not to be noticed.

HttpRequest querystring, the type of the form property is NameValueCollection, and its collection type has one feature: allows multiple string values to be stored under a key .
The following code demonstrates this particular phenomenon:

protected void Page_Load (object sender, EventArgs e) {    string[] AllKeys = Request.QueryString.AllKeys;    if (AllKeys. Length = = 0)        Response.Redirect (            request.rawurl + "? aa=1&bb=2&cc=3&aa=" + Httputility.urlencode (" 5,6,7 "), true);    StringBuilder sb = new StringBuilder ();    foreach (string key in AllKeys)        sb. AppendFormat ("{0} = {1}<br/>",             httputility.htmlencode (key), Httputility.htmlencode (request.querystring[ Key]));    This.labResult.Text = sb. ToString ();}
HttpResponse

We have only one final destination for processing HTTP requests: . and are called HttpResponse methods . It provides the functionality to focus on manipulating the HTTP response section, such as: Response flow, response header.
I give a brief list of some of the members that I think are important:

Gets the cache 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 the response Cookie collection. Public httpcookiecollection Cookies {get;} Gets or sets a wrapper filter object that is used to modify the HTTP entity principal 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 a new URL and specifies whether the 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 without buffering the file in memory. public void TransmitFile (string filename);//writes System.Object to the HTTP response stream. public void Write (object obj);

  

These members have simple explanations and should understand them.

Please pay attention to attribute statuscode here. We often use jquery to implement Ajax, such as: Using the Ajax () function, although you can set the error callback function, but it is very likely that the service side even if the yellow pages, will not trigger this callback function, unless it is set datatype= "JSON", when parsing fails , it will trigger this callback function, if it is datatype= "html", even if it is the yellow pages, it can be "normal display."
What to do? when an exception on the server does not return the correct result, set the StatusCode property, for example: Response.statuscode = $;

. NET three core objects

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.