As well as hopefully helping other people out, one objectives of this blog is to act as an aide-memoire as I often find myself looking up the same things on Google over and over again, or thinking “I knew how to do that two months ago”. So, I’ve included a category called “Aides-Memoires” which contains posts only intended to jog my memory so that I don’t have to keep repeating the same Google searches. This post is the first of this type.
<link rel="stylesheet" type="text/css" href="styles/myStyles.css" />
If you’re lucky enough to be using ASP.NET MVC you also have the option of getting the .NET Framework to work out the relative paths to the files you need to reference. If you’re using the Razor engine, this is coded as follows:
<link rel="stylesheet" type="text/css" href="@Url.Content("~/styles/myStyles.css")" />
The tilde (~) simply means “start from the root level of my website”. This is a far less brittle solution.
I’ve just read that the Razor version 2 engine in ASP.NET MVC 4 is smart enough to not need the @Url.Content() helper. It automatically resolves tildas, so the following will suffice:
<link rel="stylesheet" type="text/css" href="~/styles/myStyles.css" />
As well as being robust when files are moved around, an added benefit is that Intellisense still works correctly.