+ All Categories
Home > Education > Asp.net tips

Asp.net tips

Date post: 24-May-2015
Category:
Upload: actacademy
View: 1,262 times
Download: 2 times
Share this document with a friend
Description:
Act Academy provides .net training and course with 100% jobs assurance
53
ASP.NET Tips and Tricks
Transcript
Page 1: Asp.net tips

ASP.NET Tips and Tricks

Page 2: Asp.net tips

What we will cover Development Tips and Tricks Error Handling Tips and Tricks Production Tips and Tricks

Page 3: Asp.net tips

Session Prerequisites

Level 300Level 300

ASP Basic knowledge of ASP.NET

Page 4: Asp.net tips

Agenda

Development Tips and Tricks Error Handling Tips and Tricks Production Tips and Tricks

Page 5: Asp.net tips

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 <input type=file> server control

HttpPostedFile Object: HttpPostedFile.InputStream HttpPostedFile.ContentType HttpPostedFile.FileName HttpPostedFile.SaveAs(fileLocation)

Page 6: Asp.net tips

<html><script language="VB" runat=server> Sub Btn_Click(Sender as Object, E as EventArgs) UploadFile.PostedFile.SaveAs("c:\foo.txt") End Sub</script>

<body> <form enctype="multipart/form-data" runat=server> Select File To Upload: <input id="UploadFile" type=file runat=server> <asp:button OnClick="Btn_Click“ runat=server/> </form></body></html>

Development Tips And Tricks File upload example

Page 7: Asp.net tips

Demonstration 1File Upload

Page 8: Asp.net tips

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

Development Tips And Tricks Richer file upload

Page 9: Asp.net tips

Demonstration 2File Upload With SQL

Page 10: Asp.net tips

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

Development Tips And Tricks Image generation

Page 11: Asp.net tips

Demonstration 3Image Generation

Page 12: Asp.net tips

ASP.NET <asp:xml runat=server> 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

Development Tips And Tricks ASP.NET XML Server Control

Page 13: Asp.net tips

<html> <body>

<asp:xml id="MyXml1" DocumentSource="SalesData.xml" TransformSource="SalesChart.xsl" runat=server />

</body></html>

Development Tips And Tricks <ASP:XML> file sample

Page 14: Asp.net tips

Demonstration 4Static XML

Page 15: Asp.net tips

<%@ Page ContentType="text/xml" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SQLClient" %><%@ Import Namespace="System.Xml" %>

<script language="VB" runat=server> Sub Page_Load(Sender as Object, E as EventArgs) Dim conn as New SqlConnection(connectionString) 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</script><asp:xml id="MyXml1" runat=server/>

Development Tips And Tricks <ASP:XML> data sample

Page 16: Asp.net tips

Demonstration 5Dynamically Bind XML

Page 17: Asp.net tips

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

Development Tips And Tricks App settings

Page 18: Asp.net tips

1. Create “web.config” file in app vroot:

2. To return value:Configuration.AppSettings(“dsn”)

<configuration>

<appSettings>

<add key=“dsn”

value=“localhost;uid=sa;pwd=;Database=foo”/>

</appSettings>

</configuration>

Development Tips And Tricks App settings steps

Page 19: Asp.net tips

Demonstration 6Application Settings

Page 20: Asp.net tips

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

Development Tips And Tricks Cookieless sessions

Page 21: Asp.net tips

1. Create “web.config” file in app vroot

2. Add following text:

<configuration>

<system.web>

<sessionState cookieless=“true”/>

</system.web>

</configuration>

Development Tips And Tricks Cookieless sessions steps

Page 22: Asp.net tips

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 <%@ Page SmartNavigation=“true” %> Alternatively set in web.config file

Development Tips And Tricks Smart navigation

Page 23: Asp.net tips

Agenda

Development Tips and Tricks Error Handling Tips and Tricks Production Tips and Tricks

Page 24: Asp.net tips

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

Error Handling Tips And Tricks Page tracing

Page 25: Asp.net tips

1. Add trace directive at top of page<%@ Page Trace=“True” %>

2. Add trace calls throughout pageTrace.Write(“Button Clicked”)

Trace.Warn(“Value: “ + value)

3. Access page from browser

Error Handling Tips And Tricks Page tracing steps

Page 26: Asp.net tips

Demonstration 7Page Tracing

Page 27: Asp.net tips

1. Create “web.config” file in app vroot:

2. Access tracing URL within app http://localhost/approot/Trace.axd

<configuration>

<system.web>

<trace enabled=“true” requestLimit=“10”/>

</system.web>

</configuration>

Error Handling Tips And Tricks Application tracing steps

Page 28: Asp.net tips

Demonstration 8Application Tracing

Page 29: Asp.net tips

.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

Error Handling Tips And Tricks Error handling

Page 30: Asp.net tips

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

Error Handling Tips And Tricks Application_Error

Page 31: Asp.net tips

Demonstration 9Writing to NT Event Log

Page 32: Asp.net tips

<%@ Import Namespace=“System.Web.Mail" %><script language="VB" runat=server>

Sub Application_Error(sender as Object, e as EventArgs)

Dim MyMessage as New MailMessage

MyMessage.To = “[email protected]" MyMessage.From = "MyAppServer" MyMessage.Subject = "Unhandled Error!!!" MyMessage.BodyFormat = MailFormat.Html

MyMessage.Body = "<html><body><h1>" & Request.Path & _"</h1>" & Me.Error.ToString() & "</body></html>"

SmtpMail.Send(MyMessage)

End Sub</script>

Error Handling Tips And Tricks Sending SMTP mail

Page 33: Asp.net tips

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

Error Handling Tips And Tricks Custom errors

Page 34: Asp.net tips

1. Create “web.config” file in app vroot:<configuration>

<system.web>

<customErrors mode=“remoteonly”

defaultRedirect=“error.htm”>

<error statusCode=“404”

redirect=“adminmessage.htm”/>

<error statusCode=“403”

redirect=“noaccessallowed.htm”/>

</customErrors>

</system.web>

</configuration>

Error Handling Tips And Tricks Custom error page steps

Page 35: Asp.net tips

Demonstration 10Custom Errors

Page 36: Asp.net tips

Agenda

Development Tips and Tricks Error Handling Tips and Tricks Production Tips and Tricks

Page 37: Asp.net tips

Per Application Performance Counters Enable easily monitoring of individual

ASP.NET applications

Custom Performance Counters APIs Now possible to publish unique application-

specific performance data Great for real-time application monitoring Example: total orders, orders/sec

Production Tips And Tricks Performance counters

Page 38: Asp.net tips

Demonstration 11Performance Counters

Page 39: Asp.net tips

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

Production Tips And Tricks Process model recovery

Page 40: Asp.net tips

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

Production Tips And Tricks Reliable session state

Page 41: Asp.net tips

1. Start ASP State Service on a machine net start aspnet_state

2. Create “web.config” file in app vroot, and point it at state service machine:

<configuration>

<system.web>

<sessionState mode=“StateServer” stateConnectionString=“tcpip=server:port” />

</system.web>

</configuration>

Production Tips And Tricks External session state steps

Page 42: Asp.net tips

Session Summary Very easy to implement Little or no coding required for most Enhances the reliability of your apps

Page 43: Asp.net tips

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

Page 44: Asp.net tips

MSDN For Everyone

Enterprise ArchitectEnterprise Architect

Enterprise DeveloperEnterprise Developer

VB, C++, C#VB, C++, C#StandardStandard

ProfessionalProfessional

Visual Studio .NETVisual Studio .NET

UniversalUniversal

LibraryLibrary

Operating SystemsOperating Systems

MSDN SubscriptionsMSDN Subscriptions

ProfessionalProfessional

EnterpriseEnterprise NE

W

Page 45: Asp.net tips

.NET Developer SIG’S (CT, MA and ME) msdn.microsoft.com/usergroups

4th Tuesday of each month (CT) CT http://www.ctmsdev.net.

2nd Wed of each month (MA and ME) ME http://www.mainebytes.com Feb 21 – ASP.NET Tips and Tricks MA http://www.idevtech.com

VB.NET 1st Thursday (MA) MA http://www.nevb.com

BACOM 2nd Monday (MA) MA http://www.bacom.com

Share Point Server (MA) 4th Tuesday MA http://www.starit.com/sughome First meeting Feb 4 – 7pm

Page 46: Asp.net tips

.NET Developer SIG’S (NH, VT, RI) msdn.microsoft.com/usergroups

.NET user groups in NH http://www.nhdnug.com http://www.jjssystems.net

.NET user group in Burlington VT First meeting Feb 11 – 6PM – ASP.NET Tips and tricks

.Net user group for RI Details coming

Page 47: Asp.net tips

Do you have a very large shop? See Russ ([email protected]) or Bill ([email protected])

about .NET Readiness offer.PARTNERS HEALTH CARE INC

RAYTHEON COMPANY

STAPLES

TEXTRON INC

THOMSON CORPORATION

TYCO INTERNATIONAL

UNITED HEALTHCARE CORP

UNITED TECHNOLOGIES CORPORATION

And more…

A B B ASEA BROWN BOVERI INC

AETNA

CIGNA

C V S

E M C CORPORATION

GETRONICS

GILLETTE COMPANY

INVENSYS PLC

NORTHEAST UTILITIES

Page 48: Asp.net tips

If you’re an ISV….

email Joe Stagner at [email protected]  and introduce yourself!

Page 49: Asp.net tips

.NET Blitz program For your development staff with a minimum size of 10 developers in

over 300 pre-approved companies.

One day onsite training – (seminar format) Needs analysis 90 day MSDN Universal for 5 developers Contact [email protected] to see if your company is on the list.

Page 50: Asp.net tips

Live Webcast Events: http://www.microsoft.com/usa/webcasts/upcoming/

Architecting Web Services January 14, 2002 , 3pmMicrosoft Webcast Presents: XML Basics January 16, 2002, 3pm.NET Class Libraries From A to Z January 22, 2002, 2pm Understanding Visual Inheritance Jan 23, 2002 3pmAdvanced Web Services Using ASP .NETJanuary 29, 2002, 2PMWebservices 101January 30, 2002 3pmAdvanced XML/XSLFebruary 1, 2002 3:30pmHow To Build Mobile Solutions Using the Microsoft Mobile Internet Toolkit February 5, 2002 2pmCaching ASP.NET Application Settings February 6, 2002, 3pmBuilding a .NET Mobile Solution February 13, 2002 3pm

Page 51: Asp.net tips

Live Webcast Events: http://www.microsoft.com/usa/webcasts/upcoming/

XML and Web Enabling Legacy Applications Using BizTalk Server 2000 February 19, 2002 2pm

Advanced Webpart Development February 20, 2002 3pmHow To Develop Asynchronous Applications with .NET Framework February 26, 2002 2pmIntroducing .NET Alerts February 27, 2002 3pm

Part 1: Architecting an Application using the .NET Framework March 5, 2002 2pm

Part 2: Building a .NET Application March 12, 2002 2pm

ASP .NET - Tips and Tricks March 19, 2002 2pm

Page 53: Asp.net tips

Recommended