Vous êtes sur la page 1sur 19

Basic Response and Request Processing in ASP.

Net
IT 4203 Advanced Web Development

Jack G. Zheng Fall 2010

Overview

Dynamically generating content Processing incoming HTTP messages and user data

URL parameters HTML form data

Request and Response

Web servers and web clients communicate through HTTP messages (protocols)

Request message: client server Response message: server client

Most HTTP communications consist of a cycle of request and response


Request: getting the resource, with user data submitted to server. The server will process user data. Response: generating content and send it back to browsers.
3

ASP.Net Response Processing

Most of the HTTP response processing functionalities are provided through the System.Web.HttpResponse class

The methods and properties of the HttpResponse class are exposed through the Response property of the Page class (ASP.Net web forms). Response is a built-in object and can be directly used in .aspx pages and .aspx.cs code-behind pages.

Basic methods in ASP.Net to generate dynamic content


Response.Write() Expression code block: <%= %> Server controls


4

Response.Write

The method writes information to an HTTP response output stream.


String title = Hello; Response.Write(<h1>" +title+"</h1>");

Expression

A shortcut to print out a value or an expression


<%= [expression]%>

An expression can be a variable, formula, object property, string concatenation, method with return value, or anything that returns a value
<% String title = Hello; %> <%= Title%>

Expression code block is equivalent to Response.Write(), and are mixed with static content
6

Basic Methods to Mix Dynamic Content and Static Content (1)


1.

String concatenation: + operator


String title = Hello; Response.Write(<h1>" +title+"</h1>");

2.

Expression (spaghetti/mixed output)


<h1><%= title %></h1>

3.

String.Format()
Response.Write(String.Format(<h1>,0-</h1>, title));
7

Basic Methods to Mix Dynamic Content and Static Content (2)


4.

StringBuilder.Append()

System.Text.StringBuilder

String title = Hello; StringBuilder html=new StringBuilder(); html.Append(<h1>); html.Append(title); html.Append(<h1>); Response.Write(html.ToString());

ASP.Net Request Processing

Most of the HTTP request processing functionalities are provided through the System.Web.HttpRequest class

The methods and properties of the HttpRequest class are exposed through the Request property of the Page class (ASP.Net web forms). Request is a built-in object and can be directly used in .aspx pages and .aspx.cs code-behind pages.

Major request method type

Get

User data is sent as part of the request URL; no content in request message body Triggering actions: address bar (in browser), link, form, User data is sent in the request message body Triggering actions: form,

Post

Two basic ways to accept user input


URL parameter (Get) HTML Form (Get or Post)

URL Parameter

URL parameter
http://www.bing.com/search?q=web&form=QBLH

Request.QueryString

QueryString Property wraps URL parameters to collections of parameter names and values

Example URL: /page.aspx?para1=ibm&para2=msft


String p1 = Request.QueryString["para1"]; Use name Or String p1 = Request.QueryString[0]; Use index
Parameter values are always treated as strings
10

URL Parameters Applications

User input

http://www.google.com/search?q=asp

Data item details in a template page


http://www.newegg.com/Product/Product.aspx?Item=N82E16827135204 http://www2.cis.gsu.edu/cis/people/display.asp?pk=3

Page content selection


http://forum.java.sun.com/category.jspa?categoryID=20 http://cis3270.jackzheng.net/index.jsp?page=schedule

Settings/configuration

http://finance.yahoo.com/q/bc?s=MSFT&t=5d&l=on&z=l&q=l http://msdn.e-academy.com/elms/Storefront/Home.aspx?campus=gsu_cis

Page display/format, page content, error message,

11

HTML Form Structure


<form method="post" action=processing.aspx"> Action points to the target which processes HTTP method: form data. It can be another page/resource get or post (even in another site) or the same page. (Form elements and other normal HTML tags can be place here.) <input type="submit" value="Submit" /> </form> A submit button triggers the
submission action.
12

HTML Form Elements

Form element types


Textbox: <input type=text> Password: <input type=password> Text area: <textarea> Combo box: <select> List: <select multiple=multiple> Checkbox: <input type=text> Radio button: <input type=text> Hidden: <input type=hidden> Buttons : Submit, Reset

More details

http://www.tizag.com/htmlT/forms.php
13

HTML Form Processing

Each form element has name attribute and value attribute


name attribute parameter name value attribute parameter value

Form data can be sent either using Get or Post

Get

Form data are encoded as URL parameters Using Request.QueryString to process form data Using Request.Form
14

Post

Request.Form

Used when the HTML form method is set to post


String p1 = Request.QueryString*textbox1"+; Use parameter name Or String p1 = Request.QueryString[0];
Use index

Parameter values are always treated as strings

15

Input Validation

Server side validation


Use server side programming capability to validate user inputs Additional request/response cycles Need special handling to keep user data in the form

Client side validation


Use JavaScript to check form data before the form is submitted to the server side No additional transmission between server and browser <form onsubmit=javascript: return checkForm()>
checkForm() is a JavaScript function that either returns true or false, depending on validation results
16

Retrieving More Request and Server Information

Request object has more properties about


Brower type Client host and IP address URL information Requested resource information Referrer

Most of these can be retrieved from the Request Object


http://msdn.microsoft.com/enus/library/system.web.httprequest(v=VS.90).aspx And particularly


Request.Browser Request.Url Request.ServerVariables

17

Summary

Key concepts

HTTP request and response URL parameter HTML form, method, action HTML form element name and value attribute

Key skills

Use Request (System.Web.HttpRequest) and Response (System.Web.HttpResponse) object in ASP.Net pages. Create dynamic content and mix them with static content. Get user input through URL parameters or HTML forms.

18

More Resources

System.Web.HttpRequest

http://msdn.microsoft.com/enus/library/system.web.httprequest(v=VS.90).aspx

System.Web.HttpResponse

http://msdn.microsoft.com/enus/library/system.web.httpresponse(v=VS.90).asp x

19

Vous aimerez peut-être aussi