HTTP Message Format In ASP.NET

format

URL: Uniform Resource Identifier (Locator)

Every response contains a status code. The following are some pf the status code with its level number & meaning:

  1. 100 level: Informational (Continue)

    This means server has received the request headers, and that the client should proceed to send the request body.

  2. 200 level: Successful (OK)

    This request has succeeded, and the resulting resources (e.g. file or script output) is returned in the message body.

  3. 300 level : Redirection

    Multiple choices: It indicates that the future action needs to be taken by user agent in order to fulfil the request. It indicates multiple options for the resource that the client may need to follow.

  4. 400 level: Client Error (401 == Bad request)

     Client error.

  5. 404 level: Not found

    The requested resource does not exist.

  6. 404.10: Request header too long.

  7. 404.13: Content length too large.

  8. 404.14: URL too long.

  9. 404.15: Query string too large.

  10. 500 level: Server Error (503 == Service Unavailable)

An unexpected server error. The most common cause is server side script that has bad syntax, fails, or otherwise cannot run correctly.

Step 1: Create an application Visual Studio 2013. Now add one web form with the following code (or you can drag drop controls from ToolBox also),

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="View_State.aspx.cs" Inherits="State_Mangement.View_State" %>  
  2.    <!DOCTYPE html>  
  3.    <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.    <head runat="server">  
  6.      <title></title>  
  7.    </head>  
  8.   
  9.    <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  13.           <asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />  
  14.         </div>  
  15.      </form>  
  16.    </body>  
  17.    </html>  
Step 2: Add code in page load & button click event as,
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         TextBox1.Text = "0";  
  6.     }  
  7. }  
  8. protected void Button1_Click(object sender, EventArgs e)  
  9. {  
  10.     int click_Count = Convert.ToInt32(TextBox1.Text) + 1;  
  11.     TextBox1.Text = click_Count.ToString();  
  12. }  
Step 3: Now run our application & press F12, you will see the following,

viewstate

In above image we can see HTTP Message contains URL, Address, Status Code, Request & Response headers.

Here you can see status code 200.

Step 4: Now add two web forms, out of which one will take input from user in textbox & will pass text box values using query string to another form,
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Query_String_Form_1.aspx.cs" Inherits="State_Mangement.Query_String_Form_1" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.    <body>  
  10.      <form id="form1" runat="server">  
  11.         <div>  
  12.             First Name:  
  13.              <asp:TextBox ID="TextBox1" runat="server" Columns="50" Rows="5" TextMode="MultiLine">
  14.              </asp:TextBox>  
  15.               <br />  
  16.               <br />  
  17.               <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
  18.           </div>  
  19.       </form>  
  20.     </body>  
  21.   
  22.     </html>  
Step 5: Add the following code for first web form in code behind file & button click event,
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     int total_chars = TextBox1.Text.Length;  
  4.     Response.Redirect("Query_String_Form_2.aspx?FirstName=" + TextBox1.Text);  
  5. }  
 Add the following code for second web form in Page Load event,
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     string f_name = Request.QueryString["FirstName"].ToString();  
  4.     Label1.Text = f_name;  
  5. }  
Step 6: Now run the application. I have placed one debugger here to understand how many characters we are going to enter here.

querystring

querystring2

Now our output will look like the following with status code.

status code

Step 7: Now if I enter more characters then the following will be visible,

characters

I have entered 2040 characters then you can see what happens in the output.

Here you can see status code 404.15.

404.15

Step 8: Now add another web form & take one button from tool box & add code on button click as in the following code snippet,
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     Response.Redirect("my_page.aspx");  
  4. }  
Step 9: Now run this web form

pagenotfound

Click on button & see the output.

Here you can see status code 404.

404

For more understanding you can download the code.

Summary:

This article will help fresher candidates to understand HTTP Message Format in ASP.NET MVC 5.