Title: .NET
1ASP.NET Tips and Tricks Peter Ty Devel
oper Evangelist
.NET and Developer Group
2Agenda
- Development Tips and Tricks
- Error Handling Tips and Tricks
- Production Tips and Tricks
3Development Tips And Tricks File upload
- ASP.NET provides built-in file upload support
- No posting acceptor required
- No third party components required
- Accessible through two APIs
- Request.Files collection
- server control
- HttpPostedFile Object
- HttpPostedFile.InputStream
- HttpPostedFile.ContentType
- HttpPostedFile.FileName
- HttpPostedFile.SaveAs(fileLocation)
4Development Tips And Tricks File upload - Notes
- ASP.NET provides built-in file upload support
- No posting acceptor required
- No third party components required
- Accessible through two APIs
- Request.Files collection
- server control
- HttpPostedFile Object
- HttpPostedFile.InputStream
- HttpPostedFile.ContentType
- HttpPostedFile.FileName
- HttpPostedFile.SaveAs(fileLocation)
5Development Tips And Tricks File upload example
-
-
- Sub Btn_Click(Sender as Object, E as EventArgs)
- UploadFile.PostedFile.SaveAs("c\foo.txt")
- End Sub
-
-
- runatserver
- Select File To Upload
- runatserver
- runatserver/
-
-
6Demonstration 1File Upload
7Development Tips And Tricks Richer file upload
- File system is not the only option
- Example Storing within SQL
- Access uploaded file as byte array
- Store file within SQL as image (blob)
- Store ContentType and ContentLength also
- Provide Edit Link to display page
- Edit Link Page sets ContentType header and then
writes binary array back to client
8Demonstration 2File Upload With SQL
9Development Tips And Tricks Image generation
- Rich server image generation
- Additionally supports resizing cropping
- Overlays on top of existing images
- Read/Write Any Standard IO Stream
- System.Drawing
- Dynamically generate GIFs/JPGs from .aspx
- Set ContentType appropriately
- Optionally output cache results
10Demonstration 3Image Generation
11Development Tips And Tricks ASP.NET XML Server
Control
- ASP.NET
- Enables output of XML
- Enables optional XSL/T transform of XML
- Binding options
- File system
- Database item
- Built-in caching
- Ensure efficient re-use
- Improves performance
12Development Tips And Tricks file sample
-
-
-
- DocumentSource"SalesData.xml"
- TransformSource"SalesChart.xsl"
- runatserver /
-
13Demonstration 4Static XML
14Development Tips And Tricks data sample
-
-
-
-
-
- Sub Page_Load(Sender as Object, E as
EventArgs)
- Dim conn as New SqlConnection(connectionStri
ng)
- Dim cmd as New SqlDataAdapter(select
from products", conn)
- Dim dataset As New DataSet()
- cmd.Fill (dataset, "dataset")
- Dim XmlDoc as XmlDocument New
XmlDataDocument(dataset)
- MyXml1.Document XmlDoc
- End Sub
-
15Demonstration 5Dynamically Bind XML
16Development Tips And Tricks App settings
- Application specific settings
- Stored in web.config files
- Enables devs to avoid hard-coding them
- Administrators can later change them
- Examples
- Database Connection String
- MSMQ Queue Servers
- File Locations
17Development Tips And Tricks App settings steps
- Create web.config file in app vroot
- To return value
- Configuration.AppSettings(dsn)
sn valuelocalhostuidsapwdDataba
sefoo/
18Demonstration 6Application Settings
19Development Tips And Tricks Cookieless sessions
- Session State no longer requires client cookie
support for SessionID
- Can optionally track SessionID in URL
- Requires no code changes to app
- All relative links continue to work
20Development Tips And Tricks Cookieless sessions
steps
- Create web.config file in app vroot
- Add following text
ssionState cookielesstrue/
21Development Tips And Tricks Smart navigation
- Eliminates browser flicker/scrolling on browser
navigation
- Smooth client UI but with server code
- Automatic down-level for non-IE browsers
- No client code changes required
-
- Alternatively set in web.config file
22Agenda
- Development Tips and Tricks
- Error Handling Tips and Tricks
- Production Tips and Tricks
23Error Handling Tips And Tricks Page tracing
- ASP.NET supports page and app tracing
- Easy way to include debug statements
- No more messy Response.Write() calls!
- Great way to collect request details
- Server control tree
- Server variables, headers, cookies
- Form/Querystring parameters
24Error Handling Tips And Tricks Page tracing steps
- Add trace directive at top of page
-
- Add trace calls throughout page
- Trace.Write(Button Clicked)
- Trace.Warn(Value value)
- Access page from browser
25Demonstration 7Page Tracing
26Error Handling Tips And Tricks Application
tracing steps
- Create web.config file in app vroot
- Access tracing URL within app
- http//localhost/approot/Trace.axd
ce enabledtrue requestLimit10/
27Demonstration 8Application Tracing
28Error Handling Tips And Tricks Error handling
- .NET provides unified error architecture
- Runtime errors done using exceptions
- Full call stack information available w/ errors
- Can catch/handle/throw exceptions in any .NET
Language (including VB)
- ASP.NET also provides declarative application
custom error handling
- Enable programmatic logging of problems
- Automatically redirect users to error page when
unhandled exceptions occur
29Error Handling Tips And Tricks Error handling -
Notes
- .NET provides unified error architecture
- Runtime errors done using exceptions
- Full call stack information available w/ errors
- Can catch/handle/throw exceptions in any .NET
Language (including VB)
- ASP.NET also provides declarative application
custom error handling
- Enable programmatic logging of problems
- Automatically redirect users to error page when
unhandled exceptions occur
30Error Handling Tips And Tricks Application_Error
- Global application event raised if unhandled
exception occurs
- Provides access to current Request
- Provides access to Exception object
- Enables developer to log/track errors
- Cool Tip
- Use new EventLog class to write custom events to
log when errors occur
- Use new SmtpMail class to send email to
administrators
31Demonstration 9Writing to NT Event Log
32Error Handling Tips And Tricks Sending SMTP mail
Sub Application_Error(sender as Object, e as
EventArgs) Dim MyMessage as New MailMe
ssage MyMessage.To someone_at_microsoft
.com" MyMessage.From "MyAppServer"
MyMessage.Subject "Unhandled Error!!!"
MyMessage.BodyFormat MailFormat.Html
MyMessage.Body "" Re
quest.Path _ "" Me.Error.ToString()
"" SmtpMail.Send(MyMessag
e) End Sub
33Error Handling Tips And Tricks Custom errors
- Enable easy way to hide errors from end-users
visiting a site
- No ugly exception error messages
- Enables you to display a pretty site under
repair page of your design
- Custom Errors configured within an application
web.config configuration file
- Can be configured per status code number
- Can be configured to only display remotely
34Error Handling Tips And Tricks Custom error page
steps
- Create web.config file in app vroot
s moderemoteonly defaultRed
irecterror.htm redirectadminmessage.htm/
noaccessallowed.htm/ /system.web
35Demonstration 10Custom Errors
36Agenda
- Development Tips and Tricks
- Error Handling Tips and Tricks
- Production Tips and Tricks
37Production Tips And Tricks Performance counters
- Per Application Performance Counters
- Enable easily monitoring of individual ASP.NET
applications
- Custom Performance Counters APIs
- Now possible to publish unique application-specifi
c performance data
- Great for real-time application monitoring
- Example total orders, orders/sec
38Demonstration 11Performance Counters
39Production Tips And Tricks Process model recovery
- ASP.NET runs code in an external worker process
aspnet_wp.exe
- Automatic Crash Recovery
- Automatic Memory Leak Recovery
- Automatic Deadlock Recovery
- Can also proactively configure worker process to
reset itself proactively
- Timer based
- Request based
40Production Tips And Tricks Reliable session state
- Session State can now be external from ASP.NET
Worker Process
- ASPState Windows NT Service
- SQL Server
- Big reliability wins
- Session state survives crashes/restarts
- Enables Web farm deployment
- Multiple FE machines point to a common state store
41Production Tips And Tricks External session
state steps
- Start ASP State Service on a machine
- net start aspnet_state
- Create web.config file in app vroot, and point
it at state service machine
e modeStateServer
stateConnectionStringtcpipserverport /
42For More Information
- MSDN Web site at
- msdn.microsoft.com
- ASP.NET Quickstart at
- http//www.asp.net
- 900 samples that can be run online
- ASP.NET discussion lists
- For good best practice reference applications,
please visit IBuySpy
- http//www.IBuySpy.com
43(No Transcript)