Presentation is loading. Please wait.

Presentation is loading. Please wait.

Application Infrastructure

Similar presentations


Presentation on theme: "Application Infrastructure"— Presentation transcript:

1 Application Infrastructure
Programming ASP.NET Application Infrastructure Copyright ©

2 Agenda Web.config Global.asax Components and the Code directory
Resources and the Resources directory

3 Web.config XML file containing configuration directives
Configuration is file-based, not registry-based Organized into hierarchical config sections Private to individual applications Maximum of one per directory Configuration inheritance propagates settings down through directory hierarchy Backed by full read/write API

4 Inside Web.config Configuration sections <system.web>
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="Northwind" connectionString="..." /> </connectionStrings> <system.web> <authentication mode="Forms"/> <membership defaultProvider="AspNetSqlProvider"/> <roleManager enabled="true" <profile defaultProvider="AspNetSqlProvider"> <properties> <add name="RecentlyViewedItems" type="RecentlyViewedItems" serializeAs="Binary" /> </properties> </profile> <compilation debug="true"/> </system.web> </configuration> Configuration sections <system.web> configuration section group

5 Key Configuration Sections
Name Description <appSettings> Contains user-defined strings <connectionStrings> Contains database connection strings <authentication> Sets the authentication type (e.g., Windows, forms, etc.) <authorization> Contains URL directives specifying who has access to which resources <compilation> Specifies run-time compilation settings <customErrors> Identifies custom error pages and related settings <profile> Defines profiles for persistent storage of per-user data

6 Defining a Global Error Page
<configuration> <system.web> <customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" /> </system.web> </configuration> Show global error page only to remote users (default) Redirect to Error.aspx when an unhandled exception occurs

7 Storing Connection Strings
Web.config <configuration> <connectionStrings> <add name="Northwind" value="Server=localhost;Database=Northwind;..." /> </connectionStrings> ... </configuration> Programmatic Loading Dim northwind As String = _ ConfigurationSettings.ConnectionStrings ("Northwind").ConnectionString Declarative Loading <asp:SqlDataSource Runat="server" ... Text='<%$ ConnectionStrings:Northwind %>' />

8 Configuration Inheritance
vroot <configuration> <system.web> <compilation defaultLanguage="c#" /> </system.web> </configuration> ASPX Web.config Subfolder1 Subfolder2 ASPX ASPX Web.config <configuration> <system.web> <compilation defaultLanguage="vb" /> </system.web> </configuration>

9 Machine.config Master ASP.NET configuration file*
Many settings "baked into" ASP.NET run-time and documented in Machine.config.defaults Other settings declared in Machine.config Modify Machine.config to apply machine-wide configuration settings Consult Machine.config.comments for documentation on supported configuration settings and syntax * Found in %Windows%\Microsoft.NET\Framework\v…\CONFIG

10 Web Site Administration Tool
GUI for managing configuration settings Invoke by requesting Webadmin.axd or with Website->ASP.NET Configuration command

11 IIS ASP.NET MMC Snap-In GUI for managing configuration settings
Invoke through Internet Services Manager in Control Panel

12 Encrypting Config Sections
Most config sections can be encrypted Beta 2 will include tool support; Beta 1 does not Encryption is transparent to applications ' Encrypt the <connectionStrings> configuration section Dim config As Configuration = _ Configuration.GetWebConfiguration (Request.ApplicationPath) Dim section As ConfigurationSection = config.Sections ("connectionStrings") If Not section.IsProtected Then section.ProtectSection ("DataProtectionConfigurationProvider") config.Update () ' Save to disk End If

13 Programming ASP.NET ASP.NET Configuration Run ProtectSection.aspx and demonstrate how to encrypt and decrypt the <connectionStrings> section of Web.config. Also demonstrate how to start the Web Site Administration Tool from Visual Studio, and how to launch the ASP.NET MMC snap-in. Demonstrate how to enable debugging using the Web Site Administration Tool and how doing so modifies Web.config. Copyright ©

14 Global.asax Repository for global event handlers
Handlers for global events fired as requests travel through ASP.NET's HTTP pipeline (per-request events) Handlers for other global events (e.g., Error, Application_Start, Application_End) Maximum of one per application Must be located in application root

15 The HTTP Pipeline Global Events (Pre-Request) HTTP Handler Request
Authenticate- Request Resolve- RequestCache PreRequest- HandlerExecute BeginRequest Authorize- Request Acquire- RequestState HTTP Handler Request Response EndRequest Update- RequestCache Release- RequestState PostRequest- HandlerExecute Global Events (Post-Request)

16 Global Event Handlers Application_EventName syntax
connects handler to event Import directives import namespaces Import Namespace="System.Data" %> <script language="VB" runat="server"> Sub Application_BeginRequest (ByVal sender As Object, ByVal e As EventArgs) Dim app As HttpApplication = CType (sender, HttpApplication) ... End Sub ' Include other event handlers as needed </script> HttpApplication class provides access to request, response, and other infrastructure

17 Programming ASP.NET Global.asax Uncomment Application_BeginRequest in Global.asax and demonstrate that requests from localhost now throw an exception (401 Not Authorized). Walk through the code in Global.asax. Copyright ©

18 Components Two component models Two ways to deploy managed components
ASP uses COM components (difficult) ASP.NET uses managed components (easy) Two ways to deploy managed components ~/bin directory - Precompiled assemblies ~/Code* directory - Autocompiled assemblies * Directory name will change to Application_Code in beta 2

19 The bin Directory Introduced in ASP.NET 1.0; supported in 2.0
Private deployment of precompiled types vroot ASP.NET automatically consults assemblies in "bin" directory to resolve type references bin Other Folders DLL

20 The Code Directory New in ASP.NET 2.0 (no analogue in 1.x)
Private deployment of autcompiled types vroot ASP.NET automatically compiles source code files in "Code" directory and uses resultant assemblies to resolve type references Code Other Folders VB

21 Resources Images, localization strings, etc. Declared in RESX files
Visual Studio provides great design-time support for RESX creation and editing RESX files in ~/Resources* directory are automatically compiled by ASP.NET Once compiled, resources can be loaded declaratively or programmatically * Directory name will change to Application_Resources in beta 2

22 The Resources Directory
New in ASP.NET 2.0 (no analogue in 1.x) Private deployment of autocompiled RESXes vroot ASP.NET automatically compiles RESX files in "Resources" directory Resources Other Folders RESX

23 Loading Resources AppResources.resx String resource
Programming ASP.NET Loading Resources AppResources.resx Greeting Hello! String resource Resource name: "Greeting" Resource value: "Hello!" Declarative Loading <asp:Label ID="Output" Runat="server" ... Text='<%$ Resources:AppResources, Greeting %>' /> Run Localize.aspx. Open it in source view and show the expressions that declaratively load string resources and use them to initialize the control properties. Show the autocompiled RESX files in the Resources directory. Then change the browser's language preference and refresh the page. Finally, point out the Culture="auto" and UICulture="auto" attributes in Page directive. Programmatic Loading Output.Text = Resources.AppResources.Greeting ' Strong typing! Copyright ©

24 Programming ASP.NET Using Resources Copyright ©

25 © 2003-2004 Microsoft Corporation. All rights reserved.
Programming ASP.NET © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. Copyright ©


Download ppt "Application Infrastructure"

Similar presentations


Ads by Google