ASP.NET development: Features, tips & tricks you should know about


In this blog post I will list features, tips and tricks that one should know about when developing ASP.NET web applications (some of them will also work for ASP.NET MVC applications). The points listed below are not sorted in any way; it’s just a conglomeration of things that only need little (programming) effort to speed up or support the development process. If you know other points that should be mentioned here, please leave me a comment.

Tip 1: SMTP setting deliveryMethod “SpecifiedPickupDirectory”

While developing an ASP.NET web application one could use the SMTP settings deliveryMethod=”SpecifiedPickupDirectory” to save all e-mails to a local directory (as .eml files) instead of sending them to a SMTP server.


  [...]
  
    
      
        
      
    
  
  [...]

Using this configuration in the development environment one neither need a running SMTP server nor one need to worry about sending any e-mail to (other) recipients. It could be reasonable to put the configuration in the machine.config file on the development machine to configure all applications to save the e-mails as files.

Tip 2: Compilation setting “optimizeCompilations”

To speed up the development process one could set the optimizeCompilations compilation setting to true. When setting this value to true only the affected files are recompiled when a change to a top-level file (for example a changed file in the bin folder) is detected. Especially when developing large web applications this could save much time because it significantly decreases the application’s start-up time.


  [...]
  
    
      [...]
    
   
  [...] 

Depending on the type of changes one made to a top-level file, using this optimization could result in run-time errors (see http://msdn.microsoft.com/en-us/library/ms366723.aspx for detailed information). To force a complete recompilation of the application just set the optimizeCompilations flag temporarily back to false.

Tip 3: IIS: app_offline.htm offline page

An easy way to (temporarily) put an ASP.NET web application offline is to create a file named app_offline.htm in the root directory of the application. The IIS web-server automatically shuts down the ASP.NET application and returns the content of the app_offline.htm (and a HTTP status code 503 “Service Unavailable”) to all new incoming requests. To support Internet Explorer 6, the app_offline.htm file needs at least 512 bytes of content (one can increase the file size for example by adding some html comments).

To start the ASP.NET application again just remove or rename the app_offline.htm file.

Note: Before .NET 4 the web-server returns a status code 404 (“Page not Found”) instead of 503 (“Service Unavailable”) when serving the app_offline.htm which could cause the web page from being removed from search engine indexes (see here). Therefore, when using an older version of the .NET framework it is recommendable to add a custom aspx page that is used as “offline page” and adding the following lines of code to set the correct HTTP status code:

Response.Status = "503 Service Unavailable";
Response.StatusCode = 503;

Tip 4: Get Page instance from HttpContext object

Use the following code snippet to get the Page instance from the (current) HttpContext object:

Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
  // Do something with the Page instance
}
else
{
  // No Page instance available 
}

Tip 5: HttpContext.Items as request-level cache

An easy way to cache arbitrary data in the scope of the current HTTP context is to use the HttpContext.Items dictionary:

HttpContext.Current.Items["MyKey"] = myObject;

A use case is shown in my blog post “ASP.NET MVC 3: How to use the Unit of Work pattern with Unity 2.0” when implementing the HttpContextLifetimeManager.

,

4 responses to “ASP.NET development: Features, tips & tricks you should know about”

  1. Hello Sir ! The Article above is appreeciatable. After implemeting the above concepts..further I got stuck in my project. Actually the problem is… after getting all those mails in a particular folder…. how I will send those mails having .eml extension onto the network i.e. client’s mail ID. Either I will need to configure the SMTP server or programatically without configuring SMTP server… any help, will be appreciated.

    Thanks

    Regards
    Virendra Bhoriwal

    Video Game Programmer

  2. Hi Virendra,
    thanks for your comment. The “SpecifiedPickupDirectory” option described above is only for debugging purposes; if you would like to send the e-mails instead of creating .eml files you should set “deliveryMethod=network” and configure your smtp server in the <mailSettings> section (see here for more details).

Leave a Reply

Your email address will not be published. Required fields are marked *