Sei sulla pagina 1di 18

ASP.

NET Tips and Tricks



What we will cover
Development Tips and Tricks
Error HandlingTips and Tricks
Production Tips and Tricks
Session Prerequisites
ASP
Basic knowledge of ASP.NET
Agenda
Development Tips and Tricks
Error Handling Tips and Tricks
Production Tips and Tricks
Development Tips And Tricks
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)
<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
Demonstration 1
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
Development Tips And Tricks
Demonstration 2
File Upload With SQL
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
Demonstration 3
Image Generation
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

<html>
<body>

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

</body>
</html>
Development Tips And Tricks
<ASP:XML> file sample
Demonstration 4
Static XML
<%@ 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
Demonstration 5
Dynamically Bind XML
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
1. Create web.config file in app vroot:





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

Potrebbero piacerti anche