Miguel A. Castro Architect IDesign Level 200 Agenda Defining ASP.NET Terms & Buzzwords A Request-to-Response Walkthrough Additional Technologies Summary.

Download Report

Transcript Miguel A. Castro Architect IDesign Level 200 Agenda Defining ASP.NET Terms & Buzzwords A Request-to-Response Walkthrough Additional Technologies Summary.

Miguel A. Castro
Architect
IDesign
Level 200
Agenda
Defining ASP.NET
Terms & Buzzwords
A Request-to-Response Walkthrough
Additional Technologies
Summary
Your Speaker
Miguel A. Castro
 .NET Architect, Developer, & Trainer
 Microsoft MVP
 ASP Insider
 Member of the INETA Speakers Bureau
 Conference Speaker
 Creator of CodeBreeze
 In IT business since 1986
ineta
Agenda
Defining ASP.NET
Terms & Buzzwords
A Request-to-Response Walkthrough
Additional Technologies
Summary
Classic ASP
Interpreted language
Included embedded scripting code mixed into
HTML
Limited to VBScript
Ran in the same process as IIS
Inherently not scalable (needed MTS)
Walking Upright
Designed by Scott Guthrie & Mark Anders
Early iteration of ASP.NET
Originally known as XSP
Not .NET-related – Java based !
Became ASP+ with the design of the CLR
Rewritten in C#
Renamed when .NET branding introduced
What is ASP.NET
A framework for developing and delivering
information & applications on the web.
Known primarily as a page framework.
A complete Request/Response management
system.
Can handle and respond to all sorts of requests
on the Internet (or an Intranet).
Characteristics of ASP.NET
Runs under its own worker process.
No longer tied to IIS.
Code execution managed by CLR.
Code-Behind model allows code separation.
Includes state handling facilities.
Provides caching functionality.
Agenda
Defining ASP.NET
Terms & Buzzwords
A Request-to-Response Walkthrough
Additional Technologies
Summary
Commonly Used Terms
Request
An HTTP query initiated by a client to a server for
the purpose of performing some action.
Response
A stream of information sent back to a client from
the HTTP server that processed the client’s request.
ASP.NET Pipeline
A series of extensible functionality points which
continue the process of a request in order to
eventually obtain a response.
Commonly Used Terms
Page Lifecycle
Another series of functionality points that form the
process of converting an ASPX page to HTML
output.
The entire page lifecycle occurs between two
pipeline points.
Control Model
The heart of how ASP.NET builds HTML from
compiled classes that represent visual components.
Agenda
Defining ASP.NET
Terms & Buzzwords
A Request-to-Response Walkthrough
Additional Technologies
Summary
What Everyone Sees
Desktop Browser
Web Server (IIS)
http://www.microsoft.com/default.aspx
Rendered HTML
Now the long version…
Convert
ASPX to
HTML
Getting it to .NET (the low level stuff)
Kernel Mode Driver:
Http API used by IIS
Web Server
Web Server
(IIS) (IIS)
http.sys
Worker Process started
(one per pool) and if
needed, AppDomain is
created.
Worker Process
(w3wp.exe) – (one per app pool)
Convert
ASPX to
aspnet_isapi.dll
HTML
Attached to the extension of the request.
AppDomain
Unmanaged DLL that loads the CLR and
routes requests to the managed runtime
classes via COM-compliant interfaces.
(one per site/VD)
ISAPIRuntime
(ProcessRequest method)
Request sent to the ASP.NET
runtime for processing.
HttpRuntime.
ProcessRequest
ASP.NET Takes Over
HttpRuntime.ProcessRequest
Accessible from now until the end
of the request processing.
Accessible through
HttpContext.Current
Each AppDomain manages
multiple instances so they do
not conflict with each other
(different users or same user
with more than one request).
This is where it starts to mean
something to you, the
developer.
HttpContext created.
This serves as an entry point into the
request, response, and other accessible
variables.
An HttpApplication instance is created.
HttpApplication
Init method starts pipeline
processing.
Entering the Pipeline
Pipeline kicked off in the Init method.
HttpApplication
BeginRequest
Performs event processing.
Checks hooks from Global.asax class.
Checks hooks from external HTTP Modules.
AuthenticateRequest / Post
AuthorizeRequest / Post
ResolveRequestCache / Post
MapRequestHandler / Post
AcquireRequestState / Post
PreRequestHandlerExecute / Post
ReleaseRequestState / Post
UpdateRequestCache / Post
LogRequest / Post
EndRequest
PreSendRequestHeaders
PreSendRequestContent
Entering the Pipeline
HttpApplication
BeginRequest
AuthenticateRequest / Post
AuthorizeRequest / Post
ResolveRequestCache / Post
MapRequestHandler / Post
AcquireRequestState / Post
PreRequestHandlerExecute / Post
ReleaseRequestState / Post
UpdateRequestCache / Post
LogRequest / Post
EndRequest
PreSendRequestHeaders
PreSendRequestContent
Pipeline Events of Interest
HttpApplication
BeginRequest
Provide URL-Rewriting functionality.
AuthenticateRequest / Post
ResolveRequestCache / Post
Repopulate
HttpContext.Current.User with
stored principal.
MapRequestHandler / Post
Page authentication occurs here.
AuthorizeRequest / Post
AcquireRequestState / Post
PreRequestHandlerExecute / Post
ReleaseRequestState / Post
Determine whether to use cached
response.
UpdateRequestCache / Post
LogRequest / Post
EndRequest
PreSendRequestHeaders
PreSendRequestContent
Any event can be
hooked in
Global.asax or in
an HTTP Module.
HTTP Modules
Classes that implement IHttpModule.
MyModule : IHttpModule
Delegate-based Properties in
HttpApplication instance can
be wired up.
Method functionality
equivalent to hooking event in
Global.asax
Event hooks in
modules checked
during pipeline
processing.
ASP.NET HTTP Modules of Interest
WindowsAuthenticationModule
FormsAuthenticationModule
UrlAuthenticationModule
FileAuthorizationModule
ServiceModel
SessionStateModule
OutputCacheModule
AuthenticateRequest
AuthenticateRequest
EndRequest
AuthorizeRequest
AuthorizeRequest
PostAuthenticateRequest
AcquireRequestState
AcquireRequestState
ReleaseRequestState
EndRequest
Module Installation
Any level in the Config chain
You do know what the Config chain is, right?
The Process Continues
HttpApplication
BeginRequest
Just before this event:
Proper HTTP Handler located based
on request’s extension.
AuthenticateRequest / Post
AuthorizeRequest / Post
ResolveRequestCache / Post
MapRequestHandler / Post
AcquireRequestState / Post
PreRequestHandlerExecute / Post
ReleaseRequestState / Post
UpdateRequestCache / Post
LogRequest / Post
EndRequest
PreSendRequestHeaders
PreSendRequestContent
Handlers provide necessary
processing in order to create
a response.
HTTP Handlers
Classes that implement IHttpHandler.
MyHandler : IHttpHandler
Determines if the handler
instance can be reused by
another request.
Functionality assigned to this
particular handler.
Remember the
HttpContext contains
all accessible variables,
including Response.
HTTP Handler Factories
Can also implement IHttpHandlerFactory.
MyHandlerFactory : IHttpHandlerFactory
Allows the instantiation of any
handler based on specific
conditioning scenarios.
Characteristics of an HTTP Handler
Access to current HttpContext.
Request is in there (as is the Response) !
Handlers are wired to extensions (aspx, etc.).
Every handler is responsible for what the
response is going to be for a request.
ASPX requests expect an HTML response.
Config requests expect a “forbidden” response.
Handler Execution
HttpApplication
BeginRequest
AuthenticateRequest / Post
AuthorizeRequest / Post
ResolveRequestCache / Post
MapRequestHandler / Post
AcquireRequestState / Post
PreRequestHandlerExecute / Post
ReleaseRequestState / Post
UpdateRequestCache / Post
LogRequest / Post
EndRequest
PreSendRequestHeaders
PreSendRequestContent
The determined handler is
processed between the
PreRequestHandlerExecute and
PostRequestHandlerExecute.
Handler Installation
Handler factory that
returns and executes the
handler that will convert
an ASPX page to HTML
output.
Any level in the Config chain
The PageHandlerFactory Class
Implements IHttpHandlerFactory.
GetHandler method returns an IHttpHandler in the
form of System.Web.UI.Page-derived class
hierarchy.
Page class implements IHttpHandler.
Page class inherits from Control.
Control contains events/methods for the “Page
Event Lifecycle”.
PageHandlerFactory creates a class structure
out of your request.
Page Class Structure
Follows the structure of an
ASP.NET Server Control.
Referred to as Page-Gen Class.
Created by the PageHandlerFactory class
Virtual class created from ASPX page.
Controls created
based on ASPX
content.
inherits
Partial class with control declarations
(class name same as code-behind).
partials with
This is why you can
tap into events
from your codebehind.
Code-behind class is other side of
partial class.
inherits
ProcessRequest
method calls
lifecycle events in
Control.
System.Web.UI.Page
inherits
implements
System.Web.UI.Control
IHttpHandler
Page Class Structure
Created by the PageHandlerFactory class
Virtual class created from ASPX page.
inherits
Partial class with control declarations
(class name same as code-behind).
partials with
Code-behind class is other side of
partial class.
inherits
System.Web.UI.Page
inherits
implements
System.Web.UI.Control
IHttpHandler
Virtual Class
Created from ASPX content
Determines
language to be
used to generate
page-gen class.
Specifies the
partial class to
serve as the codebehind.
Optional:
Name of the codefile for Visual
Studio.
Virtual Class
Created from ASPX content
LiteralControl
Converted to instances of
System.Web.UI.LiteralControl
that are added to the control tree of this class.
Remember, this class will ultimately inherit from
System.Web.UI.Control
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
Virtual Class
Created from ASPX content
LiteralControl
Program code directly
added to virtual class.
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
Virtual Class
Created from ASPX content
Converted to instances of the corresponding
server control class for each of these control
tags, that will be added to the control tree
of this class.
LiteralControl
The Form control instance is placed directly
in the control tree of the class being
created; while the TextBox and Label
LiteralControl
controls are added to the control tree of the
Form control.
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
Virtual Class
Created from ASPX content
LiteralControl
“asp” tag-prefix located in config file or Register directive.
Class with tag name located in appropriate namespace and assembly.
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
Virtual Class
Created from ASPX content
__PAGE
ctrl0
System.Web.UI.Page
LiteralControl
System.Web.UI.LiteralControl
Function Code
Control IDs
Remember: this class “ultimately”
ctrl1
System.Web.UI.LiteralControl
inherits from System.Web.UI.Page
form1
System.Web.UI.HtmlControls.HtmlForm
LiteralControl
ctrl2
System.Web.UI.LiteralControl
LiteralControl
TextBox1
ctrl3
ctrl5
LiteralControl
System.Web.UI.LiteralControl
Label1
ctrl4
System.Web.UI.WebControls.TextBox
LiteralControl
System.Web.UI.WebControls.Label
LiteralControl
System.Web.UI.LiteralControl
System.Web.UI.LiteralControl
LiteralControl
Virtual Class
Created from ASPX content
LiteralControl
This is the next class you’ll see now.
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
LiteralControl
Page rendering with Trace
Page Class Structure
Created by the PageHandlerFactory class
Virtual class created from ASPX page.
inherits
Partial class with control declarations
(class name same as code-behind).
partials with
Code-behind class is other side of
partial class.
inherits
System.Web.UI.Page
inherits
implements
System.Web.UI.Control
IHttpHandler
Partial Class
Base for virtual class and partial to code-behind
Notice the name is the same as the
page’s code-behind class.
Page Class Structure
Created by the PageHandlerFactory class
Virtual class created from ASPX page.
inherits
Partial class with control declarations
(class name same as code-behind).
partials with
Code-behind class is other side of
partial class.
inherits
System.Web.UI.Page
inherits
implements
System.Web.UI.Control
IHttpHandler
Code-Behind Class
Other side of the Control Declarations class
Inheriting from Page gives you access
to a control’s event lifecycle.
Page Class Structure
Created by the PageHandlerFactory class
Virtual class created from ASPX page.
inherits
Events for the
lifecycle come
from here.
Partial
class with
control
Remember,
this entire
class declarations
hierarchy
ProcessRequest
(class
name
same
as
code-behind)
.
comes from here.
is the handler returned by the
partials with
PageHandlerFactory class.
Code-behind class is other side of
partial class.
It is from there that
the lifecycle events
are called.
It is the HttpApplication
pipeline that
inherits
calls ProcessRequest, kicking off the
System.Web.UI.Page
Page Lifecycle.
inherits
implements
System.Web.UI.Control
IHttpHandler
Page (Control) Lifecycle
Complete List
PreInit
Init
InitComplete
CreateChildControls (IsPostBack)
LoadViewState/LoadControlState
IPostBackDataHandler.LoadPostData
PreLoad
Load
IPostBackDataHandler.RaisePostBackChangedEvent
IPostBackEventHandler.RaisePostBackEvent
LoadComplete
CreateChildControls (!IsPostBack)
PreRender
DataBind
PreRenderComplete
SaveViewState/SaveControlState
Render
Unload
Most Commonly Known Points
Initialize: OnInit method and Init event
Begin tracking ViewState: TrackViewState method
Postback only
Load View State: LoadViewState method
Load Postback Data: IPostBackDataHandler.LoadPostdata method
Load: OnLoad method and Load event
Raise Changed Events: IPostBackDataHandler.RaisePostBackChangedEvent method
Raise Postback Event: IPostBackEventHandler.RaisePostBackEvent method
PreRender: OnPreRender method and PreRender event
Save View State: SaveViewState method
Render: Render method
Unload: OnUnload method and Unload event
Control Rendering
Activated through the Control class’ Render
method.
Each control is designed to output something to
the response buffer.
Most controls output HTML.
Some controls contain others in their tree.
Control rendering involves recursively rendering
child controls.
Controls don’t need to output anything
ScriptManager
Getting a Response
“Rendering” uses an HtmlTextWriter stored in
the Response object.
Response object is part of HttpContext.
Writer sent into Render method.
After pipeline complete, contents returned up
the chain to aspnet_isapi.dll then http.sys.
Results viewed on browser.
Agenda
Defining ASP.NET
Terms & Buzzwords
A Request-to-Response Walkthrough
Additional Technologies
Summary
Postbacks
Invoked by controls on form that trigger the
form to “post”.
Another HTTP Request, but of a post-type.
Certain lifecycle methods only invoked on
postback only.
In a postback, the Request object’s Form
property contains posted data from HTML
elements (using their client IDs).
Postbacks
Initialize: OnInit method and Init event
Begin tracking ViewState: TrackViewState method
Postback only
Load View State: LoadViewState method
Load Postback Data: IPostBackDataHandler.LoadPostdata method
Load: OnLoad method and Load event
Raise Changed Events: IPostBackDataHandler.RaisePostBackChangedEvent method
Raise Postback Event: IPostBackEventHandler.RaisePostBackEvent method
PreRender: OnPreRender method and PreRender event
Save View State: SaveViewState method
Render: Render method
Unload: OnUnload method and Unload event
ASCX User Controls
Register directive defines tag prefix
and name for user control.
Usage in an ASPX page (or
another ASCX control) is just like a
server control.
src points to an ascx file in the
current web application.
During parsing, virtual class is built just like a page,
except the base is called UserControl.
Also, added to Page’s Controls collection.
UserControl also inherits from Control but does
NOT implement IHttpHandler.
Master Pages
Master directive similar to a
Page directive.
ASPX page points to a
master page file.
Code-Behind class inherits from
MasterPage, which inherits from
UserControl.
Master Pages
Control that specifies
content to be defined in
an ASPX page.
Control that wraps
content.
Each Content control corresponds
to a ContentPlaceHolder control.
Master Pages
Desktop Browser
Browse to Default.aspx
Web Server (IIS)
PageHandlerFactory commences building of virtual class.
MasterPageFile attribute encountered.
Parsing shifts to master page file and code-behind.
Control tree build and added to virtual class.
Parsing returns to ASPX page and Content controls turned to
code.
Each Content control added to the Controls collection of
corresponding ContentPlaceHolder control.
Direct Browsing
Note: you cannot browse directly to an ASCX
control or a Master page.
ASHX Handlers
SimpleHandlerFactory in charge of
returning HTTP Handler.
ASHX Handlers
PageHandlerFactory:
Parses
ASPX file, building virtual class as an HTTP Handler, with
SimpleHandlerFactory:
ProcessRequest
kicking
lifecycle.
Takes this handler
code off
andpage
returns
is as HTTP Handler.
ProcessRequest called in the pipeline
as always.
Themes & Skins
Contain server control declarations (without
IDs) whose properties overwrite those declared
on the ASPX page.
Control “registration” applies.
Either in skin file or in config file.
Applied just before Init lifecycle event.
Programmatic application MUST be made in PreInit
event.
Application using Theme attribute occurs last.
Application using StyleSheetTheme attribute
occurs first.
ASP.NET Ajax
More interactive model.
Uses JavaScript to invoke Ajax requests.
Web Service (WCF) technology can be used to
handle response.
Ships with controls and components that
encapsulate functionality.
Ajax Control Toolkit – ships separately
Community driven and supported
Undergoes Microsoft Scrutiny
Better user experience.
ASP.NET MVC
Process nearly identical to conventional
ASP.NET.
It’s still ASP.NET
Uses a URL routing engine to parse URLs and
invoke appropriate classes.
http://site/controller/action/id
Pipeline is intercepted by a module tapping
into PostResolveRequestCache event.
Request is picked up by another handler for
processing.
Controller classes are sought out and
processed (similar to code-behind classes).
Why Custom Modules
Background processes
URL rewriting
Identity persistence for custom providers (or
any other kind of persistence)
Benchmarking
Why Custom Handlers
Image watermarking
Dynamic image generation
RSS output generation
File denial or protection
Tips & Tricks
Use your own base page to house common
functionality.
If overriding On{event} methods, remember that
the event-wire-up methods in code-behind fire
when you call base On{event}.
Example: override OnLoad in a base class, Page_Load in
code-behind gets called when you call base.OnLoad.
Use <page pageBaseType= demo
Register your server controls in your
Web.Config file.
Eliminates repeat registrations in ASPX & ASCX files.
Eliminates registrations in Skin files.
Tips & Tricks
Remove HTTP Modules that you don’t
need
If file protection necessary, add file
extension (ZIP) to registered extensions in
IIS.
Bring them into the pipeline for protection
and controlled exposure.
Presentation available on my site.
Tips & Tricks
Turn ViewState off in controls that don’t need
them – especially grids.
In 4.0 the model can be reversed.
More compact in 4.0.
Override
LoadPageStateFromPersistenceMedium &
SavePageStateToPersistenceMedium to alter
where and how ViewState is saved.
Tips & Tricks
Deployment Mode
In <system.web>
<deployment retail=“true” />
Turns debugging, tracing, and detailed errors OFF
Machine.Config ONLY
Way Advanced
PageParserFilter abstract base class
Lets you govern the behavior of the ASP.NET Page
Parser
Various tips & tricks
Agenda
Defining ASP.NET
Terms & Buzzwords
A Request-to-Response Walkthrough
Additional Technologies
Summary
What To Take Away From This
ASP.NET does much more than serve pages.
Decoupled architecture allows flexible hosting.
Pipeline and Event cycle – two different things.
Extensible architecture allows opportunities for
interception and alteration.
Module and Handler model enforce
encapsulation and reusability.
Be mindful of how much is happenning behind
the scenes.
References
Great Wikipedia entry
http://en.wikipedia.org/wiki/ASP+
Article: How ASP.NET Works
http://www.westwind.com/presentations/howaspnetworks/howasp
networks.asp
Code Magazine – Nov/Dec 2005
Rick Strahl
References
Article: Truly Understanding ViewState
http://weblogs.asp.net/infinitiesloop/archive/2006
/08/03/truly-understanding-viewstate.aspx
Dave Reed
Understanding ASP.NET Internals
http://grokable.com/teched-2007-presentationslides-and-demos/
Rob Howard
References
Essential ASP.NET 2.0
Addison-Wesley
Fritz Onion & Keith Brown
My Site & Email:
www.dotnetdude.com
[email protected]
www.codeplex.com
IMPORTANT:
Tech-Ed 2009 is NOT producing a DVD
Resources
www.microsoft.com/teched
www.microsoft.com/learning
Sessions On-Demand & Community
Microsoft Certification & Training Resources
http://microsoft.com/technet
http://microsoft.com/msdn
Resources for IT Professionals
Resources for Developers
www.microsoft.com/learning
Microsoft Certification and Training Resources
Complete an
evaluation on
CommNet and
enter to win!
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should
not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,
IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.