• Like
  • Save
ASP.NET Tips and Tricks
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

ASP.NET Tips and Tricks


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
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


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