A catalogue of my discoveries in software development and related subjects, that I think might be of use or interest to everyone else, or to me when I forget what I did!

Matching Tags using Regular Expressions Balancing Groups

November 28, 2008

The regular expressions engine provided by the .NET Framework includes a new feature known as 'balancing groups'. This feature allows you to increment/decrement the match count of a named capturing group by giving the group a positive and negative match context. You can then test to see you have an equal number of matches, by testing if the group has a value (i.e. an effective zero result means the group was balanced). You can include this syntax in your match pattern, so that only the balanced result is considered a match. Microsoft don't really go into this much and only show a small example of matching opening and closing paranthesis. In my case, I wanted to match a specific chunk of HTML code in a file and then find the closing tag to matching the name of the opening tag. For example:
  <div class="targetContent">
   Something in here
   <div> Something else in here</div>
Using standard regular expressions, searching for <div class="targetContent"> to </div> can work in two ways. Non-greedy mode, matches on the </div> of the inner div. In greedy mode, it matches all the way to end of the outer div. What I wanted to do, is match on the last </div> that makes the tags balance, which can be done using balancing groups! C# Code:
pattern = "<div class=\"targetContent\">.*?((?<TAG><div).*?(?<-TAG></div>))?(?(TAG)(?!))</div>";
Effectively, what the expression does is:
  1. Start the match from the div with class="targetContent"
  2. Match any internal content
  3. Whenever it encounters another div tag, it increments the TAG count
  4. Match any nested content
  5. Whenever it encounters another closing div tag, it decrements the TAG count
  6. It becomes a match when the tag count is equal
  7. Finally match on the closing tag of our outer div
This can be applied to any XML style markup, where you have the notation of opening and closing tags.
Permalink: Matching Tags using Regular Expressions Balancing Groups

LINQ Select Distinct on Custom Class Property

November 18, 2008

The standard LINQ .Distinct() function will pass your IEnumerable items to the 'default comparer' to differenciate the items. If your IEnumerable contains objects of an arbitrary class you would ordinarily have to create a IEqualityComparer to compare the relavent property of each instance. This seems like too much work just to simply remove objects that have a duplicate property value. I came up with a workaround for this using the 'group' keyword to group your objects by the target property and to then select the first record from each group, using the 'First' extension. C# Example:
   var distinctByWeekNum = from e in allUserEntries
                           group e by e.WeekNumber into g
                           select g.First();
The above example basically selects all objects that are distinct based on the 'WeekNumber', by first grouping items with the same 'WeekNumber' together and then selecting only the first item from each group (thus dropping any duplicates).
Permalink: LINQ Select Distinct on Custom Class Property

ScriptManager Service Reference and HTTPS

November 05, 2008

If you use the ASP.NET AJAX script manager with a service reference to a local WCF service, then you may encounter problems when using HTTPS on the page. If you have not configured your service for use with HTTPS you will most likely receive 'xyzService is not defined' errors in your Javascript. This is because when you add a service reference to '~/Services/xysService.svc' the AJAX Script Manager will import '~/Services/xyzService.svc/js' (or '~/Services/xyzService.svc/jsdebug') to define the prototypes for calling the service. This will return a '404 - Page Not Found' error when using the HTTPS protocol. In order to register the service for use over HTTPS you need to create a configured binding for this:

       <binding name="webHttpsBinding">
         <security mode="Transport"></security>




    <service name="WebNs.xyzService">
    <endpoint address="" behaviorConfiguration="WebNs.xyzServiceAspNetAjaxBehavior"
     binding="webHttpBinding" bindingConfiguration="webHttpsBinding" contract="WebNs.xyzService" />
This configures your service to serve up over HTTPS. Make sure you add the HTTPS binding in IIS for this to work!
Permalink: ScriptManager Service Reference and HTTPS

ASP.NET URL Rewriting and Tilde

November 04, 2008

In ASP.NET controls you can use the tilde '~' character to signify the virtual application root of the website, for example when sourcing an image for an ImageButton. Code:
  <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="Example Button" ImageUrl="~/images/button.png" />
This is fine for most cases. However, when using URL rewriting (via HttpHandlers) it can become a problem, since ASP.NET will calculate the path to the resource based on the location of the physical page it is rendering (not the page requesed in the browser [RawUrl]). If the resource is in a directory below the page directory, then it will render a relative path to the HTML, (and not an absolute path from the app root). When your browser sees a relative path in the HTML it appends that to the current working directory to retreive the resource, which for a page being served up via a rewrite, is probably the wrong directory. For example, imagine you have the following directory structure: /MyButtonPage.aspx /images/button.png /subdir/page-served-by-httphandler-mapping-to-mybuttonpage.aspx If you view '/MyButtonPage.aspx' (containing the code from above) the output path to the image would be 'images/button.png'. Your browser will therefore retreive '/images/button.png' which is correct. However, if you view '/subdir/page-served-by-httphandler-mapping-to-mybuttonpage.aspx' (which rewrites internally the '/MyButtonPage.aspx') then ASP.NET will again render the image path as 'images/button.png' (since it is relative to the 'MyButtonPage.aspx' which was rendered internally). Your browser will now try to retrieve, '/subdir/images/button.png' which does not exist. A workaround for this problem is to not use the tilde mechanism and instead reference the absolute path to the resource in the code. e.g.
  <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="Example Button" ImageUrl="/images/button.png" />
However, this will reference from the website root, not the application root, so be careful if your site runs as a virtual application within another site.
Permalink: ASP.NET URL Rewriting and Tilde

Safe Rewriting URL Encoder

October 30, 2008

When using URL rewriting to make nice URLs without querystrings, it is useful to include a meaningful name for the page, i.e. the page title, in the URL itself. This helps usability in distinguishing pages and also aids search engines in spidering different content that may be generated by a single page on your server. For some reason, the HttpUtility.UrlEncode and HttpUtility.HtmlEncode just dont cut it for making good URLs that are readable, optimized and work on the browser/server. I have written my own class for cleaning/encoding text into a form that can be used as part of a URL that will be used in a rewriting engine. Code:
Imports System.Text.RegularExpressions

Public Class UrlEncoder
    Public Shared Function EncodeRewriteUrl(ByVal inputString As String) As String
        Dim url As String = Regex.Replace(System.Web.HttpUtility.HtmlDecode(inputString.Replace(" ", "-")), "[^a-zA-Z0-9\-]", "")
        While url.Contains("--")
            url = url.Replace("--", "-")
        End While
        Return url
    End Function
End Class
The idea here is that you pass in the text you want to use as your URL, and you receive a hyphenated URL containing only URL safe characters ready for use with your rewriting engine. Example: if.. pageId=1 pageTitle="A page about 'dogs' and 'cats' (also other things)" and.. pageUrl=UrlEncoder.EncodeRewriteUrl(pageTitle & "-" & pageId & ".aspx") then.. The page URL would be: A-page-about-dogs-and-cats-also-other-things-1.aspx
Permalink: Safe Rewriting URL Encoder

Blogger extensions and integration using ASP.NET

October 27, 2008

After recently starting this blog using my Blogger account and its FTP publishing feature I wanted a more seamless integration with my existing site for the index page of the blog. In other words, I wanted to show the latest blogs as part of my existing site using the masterpage I have in ASP.NET. To achive this goal, I have set Blogger to publish to /blog on my domain, with a proprietary filename and .html extension. I have then put a Default.aspx page in the /blog directory, which uses the site master page. This default page basically opens the .html file server side and writes the stripped out relavent HTML content to a placeholder in the ASP.NET page. I have included some of the Blogger specific meta/link tags in the .aspx page head, to enable the RSS/Atom feeds from the page and to enable the edit post controls etc. to work. As its ASP.NET you can also add extra features to the page. I have added a drop down list of the categories (Blogger labels) to use as a quick link to each category. creates a .html page for each of your labels containing links to the relevant posts, these files are stored under the 'labels' folder, which can be simply enumerated on the server. Code:
if not IsPostback then


    Dim file As String = "blog-content.html"


    dim content as string=IO.File.ReadAllText(Server.MapPath(file))
    content = content.Substring(content.IndexOf("<body>") + "<body>".Length, content.LastIndexOf("</body>") - content.IndexOf("<body>") - "<body>".Length)
    content = content.Remove(content.IndexOf("<iframe"), content.IndexOf("</iframe>") + "</iframe>".Length - content.IndexOf("<iframe"))


    'load the labels
    dim labelFiles() as string=IO.Directory.GetFiles(Server.MapPath("labels"))

    ddlCats.Items.Add(New ListItem("--Jump To Category--",""))  

    for each filename as string in labelFiles

     dim url as string=filename.substring(filename.lastindexof("\")+1,filename.length-filename.lastindexof("\")-1)

     ddlCats.Items.Add(New ListItem(url.replace(".html",""),url))


   catch ex as exception

    lblContent.Text="Error: " & ex.Message

   end try
My site only uses this method for the index page, but you could extend this idea even further to display all blog pages within your custom ASP.NET / masterpage site, by putting the /blog directory under a custom HttpHandler (if your server allows this) and rewriting the URL to your ASP.NET page. Also I would suggest using regular expressions for filtering the HTML if your server supports it. -- Edit: 25/11/2008 I have now improved the integration by using the HTML generated by my masterpage as the template on, so all the pages look like you are on my website. This means I could refine the above process of finding the correct content for each placeholder, (with the addition of a seperate placeholder for the sidebar) as the divs that contain the data have specific class names. I have also created two more aspx pages, "index by date" and "index by category" - the code behind for these simply enumerate the "archives"/"labels" folders on the server, (the same as my drop down box above). Additionally, these pages open each .html file to parse the content for all of the post titles and permalinks to create the list of links for each date/label. -- Edit: 18/10/2010 Since my blog no longer uses Blogger due to FTP publishing being cancelled this code is now redundant.
Permalink: Blogger extensions and integration using ASP.NET

Blog Launch

October 23, 2008

I've started this blog on my site to catalogue anything I think might be of use or interest to everyone else, or to me when I forget what I did!

This will most likely be software development related stuff, since this is what I do day in day out. This will mainly be ASP.NET/VB.NET/C# stuff depending on what language I am using at the time but will also have some other stuff thrown in for good measure such as SQL, C++, Assembly, reverse engineering, my bio and more..

I will be back dating a lot of my previous work, as it comes to mind that it's worth blogging, so it might appear that I have been posting since before the launch, I just prefer to keep things chronological!

Anyway hopefully it might prove useful!
Permalink: Blog Launch

Programmatically Setting the Default Button of a Textbox

October 09, 2008

In ASP.NET, setting the default button of a textbox is usually a simple task as you simply wrap the button and textbox in an asp:panel and set the 'defaultbutton' attribute on the panel. Sometimes however, it is not possible to put the button and textbox in a panel together, for example if the textbox is part of a databound template (e.g. gridview, repeater etc.) If you look at the HTML code that the asp:panel method generates, it basically attaches a javascript event handler to the 'onkeypress' of the textbox. I have wrapped this functionality, so that you can programmatically set the default button of any textbox control on your page, to any button on your page. Code:
Public Shared Sub SetDefaultButton(ByVal textbox As System.Web.UI.WebControls.TextBox, ByVal button As System.Web.UI.WebControls.IButtonControl)
 If TypeOf button Is System.Web.UI.WebControls.WebControl Then
 textbox.Attributes("onkeypress") = "javascript:if (event.keyCode == 13){ document.getElementById('" & CType(button, System.Web.UI.WebControls.WebControl).ClientID & "').click();event.cancelBubble = true;if (event.stopPropagation) event.stopPropagation();return false;}"
 End If

End Sub
Permalink: Programmatically Setting the Default Button of a Textbox

Using .NET TripleDES Crypto Provider

August 28, 2008

I recently wrote a wrapper class implementing the .NET System.Security.Cryptography.TripleDESCryptoServiceProvider to quickly allow you to encrypt a plaintext string and return the Base64 encoded string of the encrypted bytes created by the TripleDES cipher (and decrypt that string back to plaintext!). This provides a very simple way to encrypt and decrypt your data without the need to store it as a binary image. The VB.NET code is shown below:
Public Class TripleDES
        Private _k, _iv As Byte()

        Public Sub New(ByVal key As Byte(), ByVal iv As Byte())
            _k = key
            _iv = iv
        End Sub

        Public Property Key() As Byte()
                Return _k
            End Get
            Set(ByVal value As Byte())
                _k = value
            End Set
        End Property

        Public Property IV() As Byte()
                Return _iv
            End Get
            Set(ByVal value As Byte())
                _iv = value
            End Set
        End Property

        Public Function Encrypt(ByVal plaintext As String) As String
            Dim cipher As New System.Security.Cryptography.TripleDESCryptoServiceProvider()
            cipher.BlockSize = 64

            Dim cipherText As String
            Using cipherTextStream As New IO.MemoryStream()
                Using cryptoStream As New System.Security.Cryptography.CryptoStream(cipherTextStream, cipher.CreateEncryptor(_k, _iv), System.Security.Cryptography.CryptoStreamMode.Write)
                    Dim bytes As Byte() = Text.Encoding.UTF8.GetBytes(plaintext)
                    cryptoStream.Write(bytes, 0, bytes.Length)

                    cipherText = Convert.ToBase64String(cipherTextStream.ToArray())
                End Using
            End Using

            Return cipherText

        End Function

        Public Function Decrypt(ByVal ciphertext As String) As String
            Dim cipher As New System.Security.Cryptography.TripleDESCryptoServiceProvider()
            cipher.BlockSize = 64

            Dim plaintext As String
            Using plaintextStream As New IO.MemoryStream()
                Using cryptoStream As New System.Security.Cryptography.CryptoStream(plaintextStream, cipher.CreateDecryptor(_k, _iv), System.Security.Cryptography.CryptoStreamMode.Write)
                    Dim bytes As Byte() = Convert.FromBase64String(ciphertext)
                    cryptoStream.Write(bytes, 0, bytes.Length)

                    plaintext = Text.Encoding.UTF8.GetString(plaintextStream.ToArray())
                End Using
            End Using

            Return plaintext

        End Function
    End Class
To use it, simply instanciate it with your Key and Initialization Vector. Make sure your K and IV are the same when decrypting data you encrypted with them!
Permalink: Using .NET TripleDES Crypto Provider

Simple Password Generator

August 28, 2008

To generate random passwords I have written a simple class in VB.NET, which accepts a string containing valid password characters and then generates passwords of n length using the specified characters.
Public Class PasswordGenerator
        Private _pwChars As String

        Public Sub New()
        End Sub

        Public Sub New(ByVal validChars As String)
            _pwChars = validChars
        End Sub

        Public Function GeneratePassword(ByVal length As Integer) As String
            Dim password As String = ""
            Dim rndNum As New Random()

            For i As Integer = 1 To length
                password &amp;= _pwChars.Substring(rndNum.Next(0, _pwChars.Length - 1), 1)

            Return password
        End Function

    End Class
Permalink: Simple Password Generator