Monday, 26 April 2010

Disabling File Change Notification (FCN) to avoid AppDomain restarting when adding a directory

I found this code here, and it works a treat. Drop it into your Global.asax (Application_Start) and it will prevent your AppDomain restarting when a subdirectory is modified/added/deleted.

This problem reared its ugly for me recently when logged-in users were losing their sessions because of activities elsewhere on the same system that were creating directories. Problem solved!

//FIX disable AppDomain restart when deleting subdirectory

//This code will turn off monitoring from the root website directory.

//Monitoring of Bin, App_Themes and other folders will still be operational, so updated DLLs will still auto deploy.

System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);

object o = p.GetValue(null, null);

System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase);

object monitor = f.GetValue(o);

System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); m.Invoke(monitor, new object[] { }); 

Using SharpZibLib over HTTPS SSL

I've been using SharpZipLib to zip up content and stream it to the user for some time now, and it's proved very reliable, robust and easy to use.

I did however recently have a problem that turned out to be IE-specific, and only when streaming to an SSL encrypted stream. Instead of a downloaded file, users would see the this error.

"Internet Explorer cannot download download from xxxxxxxxx
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later."


The solution turned out to be simple. Prior to setting other properties (such as AddHeader) you need to clear the Response object like so:

Response.Clear()
Response.ClearHeaders()

And so now my zip and download routine looks like this:
''' 
    ''' zips up any file you send it and initiates a download
    ''' 
        Public Sub ZipAndDownload(ByVal strPath As String, ByVal strFileName As String)

        Response.Clear()
        Response.ClearHeaders()
        Response.ContentType = "application/zip"
        Response.AddHeader("Content-Disposition", "inline; filename=" & strFileName & ".zip")

        ' FastZip - zip all the file and folders
        ' and stream it through the Response OutputStream
        Dim fz As New ICSharpCode.SharpZipLib.Zip.FastZip()
        fz.CreateZip(HttpContext.Current.Response.OutputStream, strPath, True, Nothing, Nothing)

    End Sub

Wednesday, 21 April 2010

Redirect all visits to HTTPS

Today I implemented HTTPS for a site after it had already been running for some time over normal HTTP. I had to redirect all incoming traffic to its HTTPS equivalent, honouring the full path and parameters. For instance, an incoming visit to http://www.myshop.com/products.aspx?widget=1234 needed to seamlessly become https://www.myshop.com/products.apsx?widget=1234 .

I added this to the masterpage and all seems sweet:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
       If Request.IsSecureConnection = False And _
            Not Request.Url.Host.Contains("localhost") Then
            Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"))
        End If
End Sub

I also more recently had to do this in C# for a site using URL rewriting. This is how it looks:

Response.Redirect(Request.Url.Scheme.Replace("http", "https") + "://" + Request.Url.Authority + Request.RawUrl);

Monday, 12 April 2010

Evaluating the speed of your site

Here are some free tools that you can use to evaluate the speed of your site:
  • Page Speed, an open source Firefox/Firebug add-on that evaluates the performance of web pages and gives suggestions for improvement.
  • YSlow, a free tool from Yahoo! that suggests ways to improve website speed.
  • WebPagetest shows a waterfall view of your pages' load performance plus an optimization checklist.
  • In Webmaster Tools, Labs > Site Performance shows the speed of your website as experienced by users around the world.

Friday, 9 April 2010

Form submission and URL rewriting

If you're using URL rewriting you need to prevent the real URL from displaying in the browser when a form is submitted. To do this you need to tell the form what it should be doing by placing this in your page load event, or better still in your masterpage page load event.
if ( !String.IsNullOrEmpty(HttpContext.Current.Request.RawUrl ))
    {
        form1.Action = HttpContext.Current.Request.RawUrl;
    }