.NET - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

.NET

Description:

Overlays on top of existing images. Read/Write Any Standard IO Stream. System.Drawing ... Page tracing. Add trace directive at top of page – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 42
Provided by: Micr160
Category:
Tags: net | of | page | top

less

Transcript and Presenter's Notes

Title: .NET


1
ASP.NET Tips and Tricks Peter Ty Devel
oper Evangelist
.NET and Developer Group
2
Agenda
  • Development Tips and Tricks
  • Error Handling Tips and Tricks
  • Production Tips and Tricks

3
Development 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)

4
Development 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)

5
Development 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/

6
Demonstration 1File Upload
7
Development 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

8
Demonstration 2File Upload With SQL
9
Development 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

10
Demonstration 3Image Generation
11
Development 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

12
Development Tips And Tricks file sample
  • DocumentSource"SalesData.xml"
  • TransformSource"SalesChart.xsl"
  • runatserver /

13
Demonstration 4Static XML
14
Development 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

15
Demonstration 5Dynamically Bind XML
16
Development 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

17
Development Tips And Tricks App settings steps
  • Create web.config file in app vroot
  • To return value
  • Configuration.AppSettings(dsn)

sn valuelocalhostuidsapwdDataba
sefoo/

18
Demonstration 6Application Settings
19
Development 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

20
Development Tips And Tricks Cookieless sessions
steps
  • Create web.config file in app vroot
  • Add following text

ssionState cookielesstrue/


21
Development 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

22
Agenda
  • Development Tips and Tricks
  • Error Handling Tips and Tricks
  • Production Tips and Tricks

23
Error 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

24
Error 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

25
Demonstration 7Page Tracing
26
Error 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/


27
Demonstration 8Application Tracing
28
Error 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

29
Error 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

30
Error 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

31
Demonstration 9Writing to NT Event Log
32
Error 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
33
Error 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

34
Error 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
35
Demonstration 10Custom Errors
36
Agenda
  • Development Tips and Tricks
  • Error Handling Tips and Tricks
  • Production Tips and Tricks

37
Production 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

38
Demonstration 11Performance Counters
39
Production 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

40
Production 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

41
Production 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 /

42
For 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)
Write a Comment
User Comments (0)
About PowerShow.com