Your SlideShare is downloading. ×
Programming web application
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Programming web application

557
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
557
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1.  Page and Application Exception Handling Programming the Web.config File Settings Asynchronous Web Page Programming Creating a Custom HTTP Handler Using the asp.net intrinsic Objects Determining the Browser Type Accessing Web Page Headers
  • 2. There are certain Web programming tasks that areoutside the bounds of the basic page request–responsescenario. Catch unhandled exceptions at the page or application level. Read and modify settings in different configuration files. Enable asynchronous communication inside Web pages. Create a custom HTTP handler to respond to requests for nonstandard file types.
  • 3.  To catch errors at the page level, you create a Page_Error event handler inside the code for each page for which you wish to catch unhandled errors. Access the Server.GetLastError method to retrieve the last error, and then call Server.ClearError to remove the error from the queue. private void Page_Error(object sender, EventArgs e) { Trace.Write("ERROR: " + Server.GetLastError().Message); Server.ClearError(); }
  • 4.  You define an application-wide handler by adding the Application_Error method to your application’s Global.asax file. Here you typically pass the error handling onto another page that might log the error and display troubleshooting information to the user by using the Server.Transfer method. void Application_Error(object sender, EventArgs e) { //code that runs when an unhandled error occurs Server.Transfer("HandleError.aspx"); }
  • 5.  There are times when you might want to programmatically edit configuration settings. ASP.NET provides the ASP.NET Configuration application programming interface (API) for this purpose. You use a System.Configuration.Configuration class to read the Web.config file and write any changes you might make. To create a Configuration object for the current application, you can use the static class WebConfigurationManager.
  • 6.  With this class, you can read configuration sections by calling the GetSection and GetSectionGroup methods. For example, the following code sample displays the current authentication mode as defined in the <system.web><authentication> section, and then displays it in the Label1 control: AuthenticationSection section =(AuthenticationSection) WebConfigurationManager.GetSection("system.web/ authentication"); Label1.Text = section.Mode.ToString();
  • 7.  Besides accessing the <system.web> section, you can access custom application settings using the WebConfigurationManager.AppSettings collection. The following code sample demonstrates how to display the MyAppSetting custom application setting (which you could add using the ASP.NET Web Site Configuration tool) in a Label control: Label1.Text = WebConfigurationManager.AppSettings["MyAppSetting"];
  • 8.  Similarly, you can programmatically access connection strings using the WebConfigurationManager.ConnectionStrings collection: Label1.Text = WebConfigurationManager.ConnectionStrings["Northwi nd"].ConnectionString;
  • 9.  If you want to make changes, however, you must choose a specific configuration location. To do this, create an instance of a Configuration object. To create an instance of the root Web.config file that applies to all applications, call the static WebConfigurationManager.OpenWebConfiguration method and pass a null parameter to create a Configuration object. Then, use the Configuration object to create objects for individual sections. Edit values in those sections and save the changes by calling Configuration.Save.
  • 10.  Asynchronous programming is used to improve the efficiency of long-running Web pages. During busy times when multiple pages are requested simultaneously the thread pool responding to user requests makes Web pages more efficient.
  • 11. 1. Add the Async=”true” attribute to the @ Page directive.2. Create events to start and end your asynchronous codethat implementsSystem.Web.IHttpAsyncHandler.BeingProcessRequest andSystem.Web.IHttpAsyncHandler.EndProcessRequest3. Call the AddOnPreRenderCompleteAsync method to declareyour event handlers
  • 12.  An HTTP handler is code that executes when an HTTP request for a specific resource is made to the server. For example, when a user requests an .aspx page from IIS, the ASP.NET page handler is executed. When an .asmx file is accessed, the ASP.NET service handler is called. To create a custom Hypertext Transfer Protocol (HTTP) handler, you first create a class that implements the IHttpHandler interface (to create a synchronous handler) or the IHttpAsyncHandler (to create an asynchronous handler).
  • 13.  Both handler interfaces require you to implement the IsReusable property and the ProcessRequest method. The IsReusable property specifies whether the IHttpHandlerFactory object (the object that actually calls the appropriate handler) can place your handlers in a pool and reuse them to increase performance or whether it must create new instances every time the handler is needed. The ProcessRequest method is responsible for actually processing the individual HTTP requests. Once it is created, you then register and configure your HTTP handler with IIS.
  • 14. public class ImageHandler : IHttpHandler{public ImageHandler(){}public bool IsReusable{get { return false; }}public void ProcessRequest(HttpContext context){context.Response.ContentType = "image/jpeg";}}
  • 15.  For performance reasons, IIS passes only requests for specific file types to ASP.NET. For example, IIS passes requests for .aspx, .axd, .ascx, and .asmx to the Aspnet_Isapi.dll file that performs the ASP.NET processing. For all other file types, including .htm, .jpg, and .gif, ASP.NET simply passes the file from the file system directly to the client browser.
  • 16. 1.Open IIS Manager.2. Expand the nodes until you get to your site or Default WebSite. Select the node for your application.3. Double-click the Handler Mappings icon in the center paneof IIS Manager.4. In the Actions pane (right side), select Add ManagedHandler.5. In the Add Managed Handler dialog box, set the Requestpath to the file name or extension you wish to map, in thiscase, .jpg. The Type name is the class name of the HTTPhandler. If your HTTP handler is inside the App_Codedirectory, it will appear in the drop-down list.
  • 17.  Alternatively, if you are using IIS 7, you can simply configure the handler for the file extension in your Web.config file. You do not, then, need to use IIS Manager. For each file extension or file name you want to register, create an <add> element in the <configuration><system.web><httpHandlers> section of your Web.config file: <configuration> <system.web> <httpHandlers> <add verb="*" path="*.jpg" type="ImageHandler"/> <add verb="*" path="*.gif" type="ImageHandler"/> </httpHandlers> </system.web> </configuration>
  • 18.  You can use the objects inside of ASP.NET to gain access to a lot of useful information about your application, the server hosting the application, and the client requesting resources on the server. These objects are referred to as the ASP.NET intrinsic objects. They are exposed through objects like Page, Browser, Response, Request, Server, and Context. Together, these objects provide you a great deal of useful information like the user’s Internet Protocol (IP) address, the type of browser making the request, errors generated during a response, the title of a given page, and much more.
  • 19.  To display different versions of Web pages for different browsers, you will need to write code that examines the HttpBrowserCapabilities object. This object is exposed through Request.Browser. Request .Browser has many members that you can use to examine individual browser capabilities.
  • 20.  The header information of a rendered HTML page contains important information that helps describe the page. This includes the name of the style sheet, the title of the page, and metadata used by search engines. ASP.NET allows you to edit this information programmatically using the System.Web.UI.HtmlControls.HtmlHead control. This control is exposed via the Page.Header property.
  • 21.  For example, you might use this to set the title of a page dynamically at run time based on the page’s content. Page.Header.Title = "Current time: " + DateTime.Now; To set style information for the page (using the <head><style> HTML tag), access Page.Header.StyleSheet. Style bodyStyle = new Style(); bodyStyle.ForeColor = System.Drawing.Color.Blue; bodyStyle.BackColor = System.Drawing.Color.LightGray; Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, " body");