Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET server side


May 13, 2021 ASP.NET


Table of contents


On the server side

We've looked at the page lifecycle and how a page contains different controls. T he page itself is instantiated as a control object. A ll web forms are basically ASP.NET of a page class. The page class has the following extremely useful properties that correspond to the internal object:

  • Session
  • Application
  • Cache
  • Request
  • Response
  • Server
  • User
  • Tracking

We will discuss each object at the appropriate time. In this tutorial we'll explore Server objects, Request objects, and Response objects.

Server object

ASP.NET object in the server is an instance of the System.Web.HttpServerUtility class. The HttpServerUtility class provides a number of properties and methods to do different things.

The properties and methods of the Server object

The methods and properties of the HttpServerUtility class are exposed ASP.NET server object provided by the server and the server.

The following table provides a series of properties for the HttpServerUtility class.

Property Describe
MachineName The name of the server computer
ScriptTimeOut Gets and sets the value of the request timeout in seconds

The following table provides some important methods:

Method Describe
CreateObject(String) Create an instance of a COM object that is validated by its ProgID.
CreateObject(Type) Create an instance of a COM object that is validated by its Type.
Equals(Object) Determines whether the specific object is consistent with the existing object.
Execute(String) The virtual path specified by the processing application is executed in the context of the current request.
Execute(String, Boolean) Execute the virtual path specified by the handler in the context of the current request, specifying whether to clear queryString and the form collection.
GetLastError Returns the previous exception.
GetType Gets the type of existing instance.
HtmlEncode Turn a normal string into a legitimate HTML string.
HtmlDecode Transform an Html string into a normal string.
Tostring Returns a string that represents the current object.
Transfer(String) For the current request, terminate the execution of the current page and start executing a new page by specifying the URL path of the page.
UrlDecode Transform a URL string into a normal string.
UrlEncodeToken The same as UrlEncode, but in an array of bytes, it contains data encoded in Base64.
UrlDecodeToken Works the same as UrlDecode, but in an array of bytes, contains data encoded in Base64.
MapPath Returns the physical path that corresponds to the file path on the specified virtual server.
Transfer Transfer execution to another web page on the current application.

The Request object

The request object is an instance of the System.Web.HttpRequest class. It represents the values and properties of the HTTP request, allowing the page to load into the browser.

The information rendered by this object is encapsulated in a higher level of abstraction (web control model). However, this object can help check some information, such as the client browser and the information logger.

The properties and methods of the Request object

The following table provides some notable properties for requesting objects:

Property Describe
AcceptTypes Gets an array of strings of user-supported MIME acceptance types.
ApplicationPath Get the real application root ASP.NET the application on the server.
Browser Get or set up information about requesting the user's browser capabilities.
ContentEncoding The entity that gets or sets the character set.
ContentLength Specifies the length of the content sent by the client in bytes.
ContentType Gets or sets the MIME content type for incoming requests.
Cookies Gets a collection of cookies sent by the client.
FilePath Gets the true path to the current request.
Files Get a collection of client upload files in a multi-part MIME format.
Form Gets a collection of form variables.
Headers Gets a collection of HTTP titles.
HttpMethod Get user-used HTTP data transfer methods (e.g. GET, POST, or HEAD)
InputStream Gets the entity content of the incoming HTTP.
IsSecureConnection Gets a value that indicates whether the HTTP connection uses a secure socket (i.e. HTTPS).
QueryString Gets a collection of HTTP inquiry string variables.
RawUrl Gets the original URL of the current request.
RequestType Gets or sets the HTTP data transfer method (GET or POST) that is used by the user.
ServerVariables Gets a collection of Web server variables.
TotalBytes Gets the number of bytes of an existing input stream.
Url Get information about the URL of an existing request.
UrlReferrer Gets URL information about previous requests from clients linked to existing URLs.
UserAgent Gets the original user agent string for the client browser.
UserHostAddress Gets the IP host address of the remote client.
UserHostName Gets the DNS name of the remote client.
UserLanguages Gets an array of sort strings for client language preferences.

The following table provides some important methods:

Method Describe
BinaryRead A binary reading of a specified number of bytes is performed from the current input stream.
Equals(Object) Decides whether the specified object is equivalent to an existing object. (inherited from an object)
GetType Gets the type of existing instance.
MapImageCoordinates Draw the incoming elephant field form parameters to the appropriate x-coordinate and y-coordinate values.
MapPath(String) Draws the specified true path as a physical path.
SaveAs Save as an HTTP request on the hard drive.
Tostring Returns a string that represents an existing object.
ValidateInput Causes validation to occur by accessing a collection of cookies, Forms, and QueryString properties.

The Response object

The response object represents the server's response to a user request. It is an example of the System.Web.Httpsponse class.

In ASP.NET, response objects play no important role in sending HTML text to users because server-side controls have nested, object-oriented methods to render themselves.

However, the HttpResponse object provides important features such as cookie features and the Redirect() method. T he Response.Redirect() method allows users to be transferred to another page, between inside or outside the application. It requires a round trip.

The properties and methods of the Response object

The following table provides some notable properties for response objects:

Property Describe
Buffer Gets or sets a value that indicates whether the output is buffered and sent after the full responder ends.
BufferOutput Gets or sets a value, whether the table name buffers the output, and sends it when the full page ends in town.
Charset Gets or sets the HTTP character set for the output stream.
ContentEncoding Gets or sets the HTTP character set for the output stream.
ContentType Gets or sets the HTTP MIME type of the output stream.
Cookies Get the appropriate collection of cookies.
Expires Gets or sets the number of minutes before the expiration of a cached page on a browser.
ExpiresAbsolute Gets or sets the absolute date and time to remove cached information from the cache.
HeaderEncoding Gets or sets a encoding object that represents the encoding of an existing title output stream.
Headers Gets a collection of response titles.
IsClientConnected Gets a value that indicates whether the user is still connected to the server.
Output Make the output text into the output of the HTTP response stream.
OutputStream Makes the binary output to the output of the HTTP content body.
RedirectLocation Gets or sets the value of the Http title location.
Status Set the status bar and return it to the client.
StatusCode Gets or sets the HTTP output status code that is returned to the client.
StatusDescription Gets or sets the HTTP output state string returned to the client.
SubStatusCode Gets or sets a status code that limits the response.
SuppressContent Gets or sets a value indicating whether http content is sent to the client.

The following table provides some important methods:

Method Describe
AddHeader Add an HTTP title to the output stream. AddHeader is provided for compatibility with earlier versions of ASP.
AppendCookie The infrastructure adds an HTTP cookie to the collection of internal cookies.
AppendHeader Add an HTTP title to the output stream.
AppendToLog Add custom log information to the InterNET Information Service (IIS) log file.
BinaryWrite Write a string of binary characters to the HTTP output stream.
ClearContent Clears all content output from the buffer stream.
Close Close the client socket.
End Send all existing buffer output to the client, stop page execution, and raise the EndRequest event.
Equals(Object) Determines whether the specified object is equivalent to an existing object.
Flush Send all existing buffer output to the client.
GetType Gets the type of existing instance.
Pics Attach an HTTP PICS-Label title to the output stream.
Redirect(String) Redirect the request to a new URL and specify a new URL.
Redirect(String, Boolean) Redirect the client to a new URL. Specify the new URL and specify whether the existing page should be terminated.
SetCookie Update existing cookies in the cookie collection.
Tostring Returns a string that represents an existing object
TransmitFile(String) Write the specified file directly into an HTTP response output stream and do not need to buffer in memory.
Write(Char) Write a character into an HTTP response output stream.
Write(Object) Write an object into an HTTP response stream.
Write(String) Write a string into an HTTP response output stream.
WriteFile(String) Write the contents of the specified file directly into an HTTP response output stream as a file block.
WriteFile(String, Boolean) Write the contents of the specified file directly into an HTTP response output stream as a block of memory.

Example

The following simple example is a text box control that allows users to enter a name, a button that sends information to the server, and a label control that displays the URL of the client computer.

Content file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
   Inherits="server_side._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

   <head runat="server">
      <title>Untitled Page</title>
   </head>

   <body>
      <form id="form1" runat="server">
         <div>

            Enter your name:
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
            <br />
            <asp:Label ID="Label1" runat="server"/>

         </div>
      </form>
   </body>

</html>

Button1_Click click on the code:

protected void Button1_Click(object sender, EventArgs e) {

   if (!String.IsNullOrEmpty(TextBox1.Text)) {

      // Access the HttpServerUtility methods through
      // the intrinsic Server object.
      Label1.Text = "Welcome, " + Server.HtmlEncode(TextBox1.Text) + ". <br/> The url is " + Server.UrlEncode(Request.Url.ToString())
   }
}

Run the page to see the following results:

ASP.NET server side