Contents n Revisit HTTP Communications between Servers and

  • Slides: 91
Download presentation
Contents n Revisit HTTP Communications between Servers and Browsers n A web of knowledge

Contents n Revisit HTTP Communications between Servers and Browsers n A web of knowledge l Class library n Asp. Net n Organize your code l Projects u Versioning p p p Dependency Self-Containing Contextual and Propagation l Team work in VS l Testing n Deployment n The Future

ASP. net http: //asp. net

ASP. net http: //asp. net

ASP. NET基础 n. NET的设计目标 n. NET Framework技术架构 n ASP. NET概述 n 安装与配置. NET运行环境 n

ASP. NET基础 n. NET的设计目标 n. NET Framework技术架构 n ASP. NET概述 n 安装与配置. NET运行环境 n Web Form

. NET Framework技术架构 n NET Framework具有两个主要组件:公共语言运行库 和. NET Framework类库

. NET Framework技术架构 n NET Framework具有两个主要组件:公共语言运行库 和. NET Framework类库

三种网站开发的框架 n Web form l Controls in Design Mode n Webpages l Razor Scripting

三种网站开发的框架 n Web form l Controls in Design Mode n Webpages l Razor Scripting n MVC

Web. Form

Web. Form

An Example

An Example

In one place <script runat=“server”> </script>

In one place <script runat=“server”> </script>

", "description": "In one place ", "width": "540" }
Code behind n Two classes are generated l My. Page u You can use

Code behind n Two classes are generated l My. Page u You can use l My. Page_aspx u Used by sys to output page

单文件页面模型 代码声明块 n 代码声明块就是把用C#语言编写的代码放在<script>标记 中。 <script runat="server"> int Day. Of. Month(int month) { int

单文件页面模型 代码声明块 n 代码声明块就是把用C#语言编写的代码放在<script>标记 中。 <script runat="server"> int Day. Of. Month(int month) { int days; switch (month) { case 1: case 3: case 5: case 12: days = 31; break; default: days = 0; break; } return days; }</script>

Page类 1. 添加客户端脚本 n 利用Client. Script. Manager类(Page类的Client. Script属性)管 理客户端脚本并将它们添加到 Web 应用程序中。 string date =

Page类 1. 添加客户端脚本 n 利用Client. Script. Manager类(Page类的Client. Script属性)管 理客户端脚本并将它们添加到 Web 应用程序中。 string date = Date. Time. Now. To. Long. Date. String(); string js = @"function show. Date(){alert('" + date + "'); }"; Page. Client. Script. Register. Client. Script. Block(this. Get. Type (), "My. Date", js, true);

访问ASP. NET内置对象 2. Request对象 n代表由客户程序发发送到服务器的所有信息,使 用它可以获取用户在浏览器中输入的信息。 int num =Convert. To. Int 32(Request. Query. String["tb"]);

访问ASP. NET内置对象 2. Request对象 n代表由客户程序发发送到服务器的所有信息,使 用它可以获取用户在浏览器中输入的信息。 int num =Convert. To. Int 32(Request. Query. String["tb"]); int num 1 = Convert. To. Int 32(Request. Form("Text. Box 1")); string ip=Request. Server. Variables["REMOTE_ADDR"];

访问ASP. NET内置对象 3. Server对象 n 可以访问服务器上的方法和属性 Response. Write(Server. Html. Encode(“<HR>”)); Response. Write(Server. Map. Path("aspbook/include

访问ASP. NET内置对象 3. Server对象 n 可以访问服务器上的方法和属性 Response. Write(Server. Html. Encode(“<HR>”)); Response. Write(Server. Map. Path("aspbook/include ")); Server. Transfer("Login. aspx");

访问ASP. NET内置对象 4. Cookie n一小段文本信息,保存在客户机的硬盘上,可由 浏览器访问。提供了一种在 Web 应用程序中存储 用户特定信息的方法。 Response. Cookies["User. Name"] ="王大力"; Response.

访问ASP. NET内置对象 4. Cookie n一小段文本信息,保存在客户机的硬盘上,可由 浏览器访问。提供了一种在 Web 应用程序中存储 用户特定信息的方法。 Response. Cookies["User. Name"] ="王大力"; Response. Cookies[“User. Name”]. Expires=Date. Tim e. Now. Add. Days(1); string name = Request. Cookies["User. Name"]. Value;

访问ASP. NET内置对象 5. Application对象 n. ASP. NET Web应用程序是单个Web服务器上的某 个虚拟目录及其子目录范围内的所有文件、页、 处理程序、模块和代码的总和 n. ASP. NET Web应用程序由Application对象表示。

访问ASP. NET内置对象 5. Application对象 n. ASP. NET Web应用程序是单个Web服务器上的某 个虚拟目录及其子目录范围内的所有文件、页、 处理程序、模块和代码的总和 n. ASP. NET Web应用程序由Application对象表示。 Application. Lock(); Application["User. Name"] = "刘大力"; Application. Un. Lock(); string name = Application["User. Name"]. To. String();

访问ASP. NET内置对象 n Application事件 l Application_Start 和Application_End l 事件处理过程存在于Global. asax文件中

访问ASP. NET内置对象 n Application事件 l Application_Start 和Application_End l 事件处理过程存在于Global. asax文件中

访问ASP. NET内置对象 6. Session对象 n Session对象用于维护会话状态。用户在一段时间内对 站点的一次访问就是一次会话。 Session["User. Name"] ="王大力"; string name = (string)Session["User.

访问ASP. NET内置对象 6. Session对象 n Session对象用于维护会话状态。用户在一段时间内对 站点的一次访问就是一次会话。 Session["User. Name"] ="王大力"; string name = (string)Session["User. Name"]; Session. Remove["User. Name"]; n 事件Session_On. Start和Session_On. End

Asp. net事件

Asp. net事件

事件模型概述 2. 事件处理过程 void Event. Method(object sender, Event. Args e) { 事件处理代码 }

事件模型概述 2. 事件处理过程 void Event. Method(object sender, Event. Args e) { 事件处理代码 }

跨页发送 n 若回送给另一个页面,则称作跨页发送。 1. 实现跨页发送 n 设置控件的Post. Back. Url属性为发送的目标页,可以实现跨 页发送。 <asp: Button ID="Ok" Runat="server"

跨页发送 n 若回送给另一个页面,则称作跨页发送。 1. 实现跨页发送 n 设置控件的Post. Back. Url属性为发送的目标页,可以实现跨 页发送。 <asp: Button ID="Ok" Runat="server" Text="Cross Page" Post. Back. Url="Another. Page. aspx" /> 2. 从源页获取信息 n Page类的Previous. Page属性提供了对源页的引用,使用 Page类的Find. Control方法,获取源页上要访问的控件。 Text. Box firstname = (Text. Box)Previous. Page. Find. Control("First. Name");

Client. Id <asp: textbox id="Text. Box 1" runat="server" text="Sample Text" client. Id=“sth” /> <input

Client. Id <asp: textbox id="Text. Box 1" runat="server" text="Sample Text" client. Id=“sth” /> <input name="Text. Box 1" type="text" value="Sample Text" id="Text. Box 1" />

The processing cycle for an ASP. NET Web page 1. 2. 3. user gets

The processing cycle for an ASP. NET Web page 1. 2. 3. user gets the page. The page dynamically renders markup to the browser. user types information or selects choices and then clicks a button. (If users click a, the page might simply navigate to another page l u 4. The page is posted to the Web server. The browser performs a POST, which is referred to as a postback. l u 5. posted back to itself, or cross-page posting On the Web server, the page runs again. l 6. 7. and no further processing takes place on the first page. ) what the user typed or selected is available to the page. The page performs the processing that you have programmed The page renders itself back to the browser

ASP. NET控件

ASP. NET控件

命名法介绍 – 骆驼与帕斯卡 n 骆驼命名法(camel. Case notation) l lbl. Post. Back, btn. Submit n

命名法介绍 – 骆驼与帕斯卡 n 骆驼命名法(camel. Case notation) l lbl. Post. Back, btn. Submit n 帕斯卡命名法(Pascal. Case notation) l Display. Info, User. Name n 匈牙利命名法(Hungarian notation) l g_Num 1, m_Display n 本课程使用camel. Case, Pascal. Case notation

Cross Pages

Cross Pages

n Submit to the same page n Submit to another page n Redirect n

n Submit to the same page n Submit to another page n Redirect n Transfer

Submit to the same page

Submit to the same page

Stateless connection n When a browser requests a page from a Web server, the

Stateless connection n When a browser requests a page from a Web server, the browser and the server are connected only long enough to process the request. l After the Web server has rendered a page to the browser, the connection is terminated.

Stateless connection n If the browser makes another request to the same Web server,

Stateless connection n If the browser makes another request to the same Web server, even for the same page, this request is processed as a new request. n each postback and round trip results in a new instance of the page.

Submit page to another

Submit page to another

Cross page posting n initiated by a user action. n using an HTTP POST

Cross page posting n initiated by a user action. n using an HTTP POST command, n In addition, if the source and target page are in the same Web application, the target page can access public properties of the source page. n all of the pages in the application can share information stored in session state or application state

Cross page posting <asp: Button ID="Button 1" Post. Back. Url="~/Target. Page. aspx" runat="server" Text="Submit"

Cross page posting <asp: Button ID="Button 1" Post. Back. Url="~/Target. Page. aspx" runat="server" Text="Submit" />

Server Transfer

Server Transfer

Sever Transfer n The page transferred to should be another . aspx page. n.

Sever Transfer n The page transferred to should be another . aspx page. n. asp or. asmx page is not valid. n preserves collections. the Query. String and Form

Sever Transfer n does not verify that the current user is authorized to view

Sever Transfer n does not verify that the current user is authorized to view the resource n You can force reauthorization by using the Redirect n The source and target pages must be in the same Web application.

 • target page can read control values and public property values from the

• target page can read control values and public property values from the source page • browser has no information about the changed page, and it retains information about the original (source) URL. • Because the transfer between source and target pages happens on the server,

 • For example, the Address box in Internet Explorer does not change after

• For example, the Address box in Internet Explorer does not change after a transfer, • and instead continues to show the URL of the page it most recently requested (which is usually the source page). • The browser's history is not updated to reflect the transfer.

Redirect at browser

Redirect at browser

Redirect n performs a client-side redirect in which the browser requests the new resource.

Redirect n performs a client-side redirect in which the browser requests the new resource. l In a browser redirect, the browser issues a new request to the target server in the form of an HTTP GET request. l You can trigger the redirect programmatically in client script or server code

In client script, you can call the form. submit method, provided the <form> element's

In client script, you can call the form. submit method, provided the <form> element's method attribute value is get. In that case, if the current page contains form data, it is passed to the target server by appending it as a query string onto the requested URL.

Response. Redirect • In server code, you can programmatically redirect by calling the Redirect

Response. Redirect • In server code, you can programmatically redirect by calling the Redirect method. • The method sends a command to the user's browser

Note: l Internet Explorer through version 6. 0 can process only up to 2,

Note: l Internet Explorer through version 6. 0 can process only up to 2, 048 characters in the URL, including data in the query string. l If the URL exceeds 2, 048 characters, Use Session state

Master page

Master page

n Like Templates l Content. Place. Holder in Template l Content in Page n

n Like Templates l Content. Place. Holder in Template l Content in Page n Templages Nestable

n Content requires Content. Place. Holder l If a content finds no content. Placehodler,

n Content requires Content. Place. Holder l If a content finds no content. Placehodler, exception will be thrown n You may consider making a master pages for your app. The master page then can in turn corroborate with an outside master. u This will make it easy to fit your app in another root app.

How to set a masterpage n <@page… Master. Page. File @> markup l Sets

How to set a masterpage n <@page… Master. Page. File @> markup l Sets the path to the master page for the content page or nested master page. l Supports relative and absolute paths. n Page. Master. Page. File in codebehind l Gets or sets the virtual path of the master page.

n Masterpagefile can be set for nested masterpage dynamically? l Seems no. n But

n Masterpagefile can be set for nested masterpage dynamically? l Seems no. n But you can do so in a page by l This. master

Configuration for whole site n Location l System. web u Pages n Note: not

Configuration for whole site n Location l System. web u Pages n Note: not to master pages

<system. web> <!--. . . --> <pages page. Base. Type="My. Web. UI. My. Page.

<system. web> <!--. . . --> <pages page. Base. Type="My. Web. UI. My. Page. Base" /> <!--. . . --> </system. web>

Webpages

Webpages

Razor n@ n @{} n @function foo(){ n}

Razor [email protected] n @{} n @function foo(){ n}

Templates n Layout n @Renderpage n @Render. Section vs @Section l @Render. Body vs

Templates n Layout n @Renderpage n @Render. Section vs @Section l @Render. Body vs content. At. Page

MVC

MVC

n Each request activates a controller n Page as a returned view. n Model

n Each request activates a controller n Page as a returned view. n Model behind a Page

The End Thank You!

The End Thank You!

Asp. net webpage overview n http: //msdn. microsoft. com/enus/library/ms 178125. aspx

Asp. net webpage overview n http: //msdn. microsoft. com/enus/library/ms 178125. aspx

n Client Script in ASP. NET Web Pages n http: //msdn. microsoft. com/enus/library/3 hc

n Client Script in ASP. NET Web Pages n http: //msdn. microsoft. com/enus/library/3 hc 29 e 2 a. aspx

n http: //www. asp. net/master-pages/tutorials/nestedmaster-pages-cs n http: //www. asp. net/master-pages/tutorials/specifyingthe-master-page-programmatically-cs

n http: //www. asp. net/master-pages/tutorials/nestedmaster-pages-cs n http: //www. asp. net/master-pages/tutorials/specifyingthe-master-page-programmatically-cs