William Duffy

Glasgow Based C# ASP.NET Web Developer

Post archive for ‘C#’

ASP.NET MVC root url’s with generic routing

Normally, the url to handle a contact view would look similar to /Home/Contact. I’m really not keen on that and wanted my top level view’s url to look like /Contact. This in itself is easy enough, you just create a route {action} and set that routes default controller value to { controller = “Root” }. [...]

JSON serialization in ASP.NET

When working with JavaScript and ASP.NET together you will no doubt find yourself having to return objects from the server side domain to the client side domain. One common method of doing this is to return XML which represents the object you wish to return. You then map the values in the XML document to [...]

Stop XSL from escaping ampersands in html entities

I had a real hard time today trying to work out why my encoded XML document was being re-encoded when rendered using XSLT. My problem was that an apostrophy ( ‘ ) in my XML was properly encoded to ’ If this is presented to the browser “as-is” then the html entity is correctly rendered [...]

How to consume an XML feed in ASP.NET – RSS

XML is the standard for distributing content on the internet, however it can prove a challenge when trying to consume and handle the data retrieved from an XML document. This is the first in a two part series on consuming and displaying XML content on your own website. We will be using XPathDocument and XPathNavigator [...]

Parsing dates when ASP.NET’s DateTime.Parse doesn’t work

Has DateTime.Parse ever thrown the following exception when you passed it a legally formatted value, leaving you bemused as to why it can’t parse it? String was not recognized as a valid DateTime As powerful as DateTime.Parse is, it cannot handle every representation of a date/time value without a little help from you, the developer. [...]

ASP.NET Request.Querystring with escaped ampersands

Take the following url “/images/myimage.jpg&width=300″. URL Encoding has no support whatsoever for HTML Encoding so passing an escaped ampersand html entity though a url in this format is illegal. Now, you may well be thinking “Hold on a minute William, I need to escape my ampersands to have valid XHTML markup on my pages”. Your [...]

ASP.NET’s Image Upload Filetype Validation

ASP.NET’s file upload facility offers a quick, easy method for allowing users to upload content to your server. In many cases however the upload type must be restricted. For example, perhaps you are allowing users to upload a profile image. You certainly don’t want them to be capable of uploading an executable (.exe) file! The [...]

Monitoring Database Connections

If you don’t close your database connections you can take down your server in a few minutes, that’s fact! But closing database connections is something that beginners can very easily forget. Here I will show you how to monitor database connections to SQL Server and ensure that your code is not draining the connection pool [...]

Coalesce: ASP.NET Strings

In programming it is a common occurrence to have a list of possiblities from which you would like the first available option selected. SQL Server has a handy method (Coalesce) which makes performing this easy, but unless you are using nullable types .NET does not have a suitable alternative. Here, I will make a helper [...]

ASP.NET’s “Internet Url” Regular Expression Validator

The regular expression validator is a great tool when a fast, robust, content validator is required. Drop it on the page, choose it’s ControlToValidate and give it an expression to match against the specified control’s content. There are even built in expressions to choose from….one of which is “Internet URL”. The problem with this is [...]