Your SlideShare is downloading. ×
0
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
ASP.NET Tips and Tricks
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

ASP.NET Tips and Tricks

6,339

Published on

http://www.intertech.com/Courses/Course.aspx?CourseID=99304 …

http://www.intertech.com/Courses/Course.aspx?CourseID=99304

This slide deck is from an Intertech (http://www.Intertech.com) presentation on ASP.NET tips and tricks.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,339
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. ASP.NET Tips and Tricks<br />
  • 2. ASP.NET 2.0 Architecture<br />Ch, Ch, Ch, Ch, Ch, Changes…<br />ASP.NET took on the most changes in .NET 2.0<br />Web site are no longer projects!<br />We now work with web sites as a collection of “dynamically compiled” web files.<br />Previously, all code-behinds and miscellaneous class files compiled into one big .DLL<br />This made web page updates painful! <br />
  • 3. ASP.NET 2.0 Architecture<br />Now, each web page is a miniature .DLL file.<br />Updating a web site now involves merely replacing the old file with the new one<br />Doesn’t matter if it’s the .ASPX or the code-behind file<br />Even class files can be added. They are also dynamically compiled when used.<br />When used with deployment, will strip source code out.<br />
  • 4. aspnet_compiler.exe<br />Wait!Isn’t putting your source code on the web server a bad idea? Isn’t that why ASP files were hacked so easily?<br />aspnet_compiler.exe fixes this problem!<br />This utility can pre-compile your entire web site and create all necessary .DLLs for deployment.<br />How did Microsoft suggest pre-compiling ASP.NET 1.1 sites? Easy – just visit every web page with your web browser… Not a real answer.<br />
  • 5. aspnet_compiler.exe<br />This utility can also be used to deploy a website from a working folder to a virtual directory.<br />It can also be used to compile in place.<br />It will pre-compile the site in the process.<br />
  • 6. Where are the files?<br />Where are the .DLLs?<br />Depends on where they are deployed!<br />C:WindowsMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files<br />Navigate here to find the DLLs<br />Open the source code (*.cs or *.vb) files to see the control declarations and the added event handlers<br />Open the a page’s DLL using ILDASM.exe<br />
  • 7. When are the DLLs recompiled?<br />Many reasons…<br />When they’re explicitly compiled using aspnet_compiler.exe<br />When a web site is compiled explicitly from Visual Studio<br />When a file changes (ASPX or code-behind) on the web site<br />Only the changed code is recompiled.<br />Restarting the web server does not throw away the DLL’s.<br />
  • 8. ASP.NET 1.1 Architecture<br />UML of page hierarchy<br />Most ASP.NET developersdon’t know this pattern!<br />The ASPX file inherits thecontrols declared in the code-behind file.<br />Page<br />_Default<br />_Default_ASPX<br />
  • 9. ASP.NET 2.0 Architecture<br />Slightly different!<br />ASP.NET takes advantage of .NET 2.0 partial classes!<br />Page<br />_Default<br />_Default<br />Hidden file. Automatically generated.Contains control declarations and event handler assignments.<br />_Default_ASPX<br />
  • 10. So… do you like it?<br />This architecture cleaned up the Code-Behind file (now called CodeFile).<br />No control declarations<br />No event handling code<br />My Opinions: I miss the control declarations – what did I name that DataGrid control?Now I use thisa lot.<br />C#: How do I create a Page_PreInit() event handler?<br />
  • 11. ASP.NET Snippets<br />Download from www.MNDeveloper.com<br />Gives C# developers access to event handlers for ASPX, MASTER, ASCX, ASMX, and ASAX files.<br />Follow installation instructions carefully!<br />If you can’t see the Snippets Manager, change your VS settings to General Development Settings.<br />
  • 12. ASP.NET Handlers and Modules<br />The old days of C++/MFC web programming…<br />ISAPI Extensions – called directly within the Querystring from the browser<br />Ebay is still doing this today: http://search.ebay.com/search/search.dll?from=R40&amp;satitle=frogger&amp;category0=<br />ISAPI Filters – ran on IIS investigating, modifying and even shutting down requests and responses passing through IIS<br />Unmanaged ISAPI Still the fastest way to program the web<br />ATL Server anyone? (What’s that?)<br />
  • 13. ASP.NET Handlers and Modules<br />ISAPI Extensions = Handlers<br />Called directly in URL<br />Offer capability of custom file extensions<br />ISAPI Filters = Modules<br />Often used for security<br />Not a good choice for modifying output (limited)<br />Can stop a Request or Response in its tracks!<br />Used extensively by ASP.NET for security, caching, session variables, etc.<br />
  • 14. ASP.NET Handlers and Modules<br />If you’re writing ASP.NET apps today, you’re already using Handlers and Modules!<br />All Handlers and Modules are registered in the root “web.config” file for .NET 2.0, located at: C:WindowsMicrosoft.NETFrameworkv2.0.50727CONFIG<br />They are in the machine.config file in .NET 1.1<br />Let’s look at them now…<br />
  • 15. ASP.NET Handlers<br />The easiest way to create you own handler is to add an ASHX file to your web site.<br />It is pre-registered to automatically pass the request into the ASHX file.Here’s some starting code.<br />&amp;lt;%@ WebHandler Language=&amp;quot;C#&amp;quot; Class=&amp;quot;Handler&amp;quot; %&amp;gt;<br />using System;<br />using System.Web;<br />public class Handler : IHttpHandler {<br /> public void ProcessRequest (HttpContext context) {<br />context.Response.ContentType = &amp;quot;text/plain&amp;quot;;<br />context.Response.Write(&amp;quot;Hello World&amp;quot;);<br /> }<br /> public boolIsReusable {<br /> get {<br /> return false;<br /> }<br />}<br />}<br />
  • 16. ASP.NET Handlers<br />ASHX files were not officially supported in VS 2003. (A bit of a mystery at the time)<br />With access to the HttpContext, you can create any kind of response that you want.<br />The IsReusable property will instruct ASP.NET whether to hold the DLL in memory for additional requests.<br />Can be much fast than ASPX files – no control tree to file through!<br />
  • 17. ASP.NET Handlers<br />Want your own file extension? Register it with IIS and ASP.NET.<br />Register with ASP.NET in web.config at root, web site, virtual directory or folder level.<br />ASP.NET 2.0 also requires that specify the &amp;lt;buildProviders&amp;gt; section for the new file extension.<br />What actually gets sent back to the client is up to you. You return data HTML your Handler code.<br />
  • 18. ASP.NET Modules<br />Perfect for implementing your own security<br />Plug right into the Http pipeline.<br />Warning: a badly written module can affect performance<br />Consider other uses for modules:<br />Tracking/Counting requests<br />Redirecting requests to different pages<br />Blacklisting troublesome IP addresses<br />
  • 19. ASP.NET Custom Controls<br />Why bother? Power!!!<br />Create your own control that emits whatever HTML/JavaScript you’d like.<br />Create a composite control that contains several controls.<br />Create a utility control without a UI that performs under the covers<br />Inherit from an existing web control and make it your own.<br />
  • 20. ASP.NET Custom Controls<br />You will inherits from the System.Web.UI.WebControls.WebControl class.<br />If no user interface needed, consider inheriting from WebControl’s parent class – Control.<br />Visual Studio project template will get you started.<br />It’s all about the attributes… See example.<br />Can be programmed to support AutoPostBack.<br />Override CreateChildControls() to create composite control containing multiple web server controls.<br />
  • 21. ASP.NET Custom Controls<br />Web Site must have reference to DLL containing custom control(s)<br />When part of a VS solution, compiling adds control to Toolbox automatically! (new in VS 2005)<br />Dragging the control onto the form automatically registers it on the ASPX, ASCX, MASTER file<br />Assembly level attribute can set Register prefix:<br />[assembly: TagPrefix(“MyControls”, “myc”)]<br />
  • 22. SqlCacheDependency Object<br />ASP.NET Caching has been around as long as .NET has been around<br />Cache object<br />OutputCaching<br />The Cache object can have dependencies placed on time, a file, a directory or another Cache object.<br />If a file changed or a Cache changed, the initial Cache is flushed.<br />Good coding practice ensured our objects were always populated.<br />
  • 23. SqlCacheDependency Object<br />File dependency is cute, real world demands database dependency!<br />Hacks were abound in the .NET 1.1 world.<br />Table triggers and extended stored procedures were writing to the file system or calling custom handlers<br />(Rightfully) scared DB Adminshated these.<br />Microsoft created the SqlDependency object to address this need.<br />Custom Dependencies can now be created using CacheDependency class, which is no longer sealed!<br />When data in a database table changes, throw away the cached data.<br />
  • 24. SqlCacheDependency Object<br />What is this supported with?<br />Sql Server 7.0, Sql Server 2000 and MSDE, Sql Server 2005 (Std, Express, Enterprise, etc.)<br />Sql Server 2005 is the only database that automatically makes external notifications if a table has changed.<br />We can make it work with the older versions as well (with a little more work).<br />Ultimately, we want to throw away our cached data when someone INSERTs, UPDATEs or DELETEs data in a given table.<br />
  • 25. Sql Server 2005 Setup<br />None! It’s built in already!<br />Just create a SqlCacheDependency object with a SqlCommand instance.<br />Sql 7.0 and 2000?<br />Let’s see how to set those up…<br />
  • 26. SqlCacheDependency Object<br />For Sql Server 7.0/2000, two primary steps are needed to create a SqlCacheDependency on a database table.<br />Run the aspnet_regsql.exe against the database.<br />This adds a tracking table called (huge frickin’ name) “AspNet_SqlCacheTablesForChangeNotification” and numerous triggers and stored procedures.<br />Run the aspnet_regsql.exe against the table you are monitoring.<br />This adds a trigger to that particular table and an entry to the table we just created (above).<br />
  • 27. Setting up our Database<br />Use the aspnet_regsql.exe utility against the database.<br />aspnet_regsql.exe –S (local) –U username –P password –d pubs –ed<br />S = Server name<br />U, P = Username and password<br />d = database name<br />ed = enable database for SQL Dependency Caching<br />
  • 28. Setting up Table<br />Use the aspnet_regsql.exe utility against the table.<br />aspnet_regsql.exe –S (local) –U username –P password –d pubs –et –t authors<br />et = enable table for SQL Dependency Caching<br />t = table name<br />
  • 29. Final Piece: web.config<br />&amp;lt;system.web&amp;gt;<br /> &amp;lt;caching&amp;gt;<br /> &amp;lt;sqlCacheDependency enabled=“true” pollTime=“1000” &amp;gt;<br /> &amp;lt;databases&amp;gt;<br /> &amp;lt;add name=“pubs” connectionStringName=“LocalPubs” /&amp;gt;<br /> &amp;lt;/databases&amp;gt;<br /> &amp;lt;/sqlCacheDependency&amp;gt;<br /> &amp;lt;/caching&amp;gt;<br />&amp;lt;/system.web&amp;gt;<br />pollTime is in milliseconds <br />connectionStringName has been predefined in connectionStringssection.<br />This isn’t needed for Sql Server 2005!<br />
  • 30. Using Our SqlCacheDependency<br />SqlCacheDependency constructor, Two versions:<br />Sql 7.0 and Sql 2000:<br />SqlCacheDependencydep = <br /> new SqlCacheDependency(“pubs”, “authors”);<br />Sql 2005:<br />SqlCacheDependencydep = <br /> new SqlCacheDependency(myCommandObj);<br />
  • 31. Performance?<br />Sql Server 2005 will always perform faster than 7.0 and 2000 implementations<br />Built into the Sql Server 2005 runtime<br />No polling necessary<br />Creating/modifying Sql 7.0/2000 tables requires admin rights to perform. <br />Not all developers have those.<br />
  • 32. My Book Recommendations<br />MSPress Programming Microsoft ASP.NET 2.0<br />Core Reference (Dino Esposito)<br />Advanced Topics (Dino Esposito)<br />MSPressDeveloping More-Secure Microsoft ASP.NET 2.0 Applications (Dominick Baier)<br />Wrox ASP.NET 2.0 MVP Hacks and Tips (David Yack, et. al)<br />Wrox Web Parts and Custom Controls with ASP.NET 2.0 (Peter Vogel)<br />

×