Hi! I'm Martin Söderlund, the owner of this blog. I work as a web consultant in Stockholm, Sweden. My main focus areas are .NET development and interface development.
If you've got something on your mind, feel free to contact me.


Thank you for visiting!

Please consider subscribing to the RSS feed or following me on Twitter.

Check if Bootstrap CSS is Loaded and Provide Local Fallback

When a CDN somehow fails, you want to make sure you can load a local fallback.

Here's a snippet to check if Bootstrap CDN is down, and then proceed to load a local Boostrap fallback version, utilizing Boostrap's hidden CSS class.

If Bootstrap is loaded, the span will be hidden. We simply check if it is hidden, and if it aint - we assume Bootstrap wasn't loaded from CDN and load our local fallback instead.

Run this script after you've tried to load Boostrap from a CDN.

var $span = $('<span class="bs hidden"></span>');
var $bs = $('body').append($span).find('.bs');

if ($bs.css('display') !== 'none') {
	$("head").append('<link rel="stylesheet" href="/css/Bootstrap/bootstrap.min.css">');

Other, more sofisticated methods, depend on libraries like yepnope or requirejs. Meanwhile, this method works just fine.

Take Ownership on Windows 10

Update: Now there's a nice tool with an interface that you can use instead. Reset Files Permission

After installing Windows 10, you suddenly don't have permissions to save in some locations. It doesn't matter if you're an administrator either.

After some research, I found a simple solution in this thread on TenForums.

Simply fire up a command prompt (hit Windows key, type CMD, right click and choose Run as administrator) and run this command on your location or drive (change E: to appropriate):

icacls "E:" /setowner "Administrators" /T /C

Depending of the number of files in your location, it might take some time.

Faster Visual Studio Attach to Process Debugging

Visual Studio's Attach to process is an alternative to the good ol' F5 which builds your solution and lets you debug it. However Attach to process can be made a little bit easier using a few helpful tricks.

First of all, we need to add an external tool to Visual Studio:

  1. Go to Tools->External Tools and press Add to create a new tool.
  2. Fill in these values in the correct field:

    Title: list w3wp
    Command: %windir%\system32\inetsrv\appcmd.exe
    Arguments: list wp

    Make sure "Use Output window" is checked and remember what order your tool has in the list, for instance 4th. Press Ok.

Add external tool in Visual Studio

Remember that your w3wp processes will be in Visual Studio's output window when using this tool!

Now we want easy access to our tool. Let's give it a shortcut command:

  1. Go to Tools->Options->Environment->Keyboard.
  2. In the Show command containing box, search for tools.external
  3. Find the correct  Tools.ExternalCommandX where X matches the order of your tool in the external tools list, for instance 4.
    Assign shortcut keys to your command and press Ok. I use Alt+E.

Assign shortcuts in Visual Studio

Next up, we need to use the helpful Visual Studio extension ReAttach. Download and install it. It helps us reattach to the same w3wp process over and over again :-) I've written about ReAttach and debugging before.

Finally, let's assign some shortcut commands to Attach to process and reattach:

Using the same technique as last time:

  1. Search for Debug.AttachtoProcess and assign Alt+W
  2. Search for Debug.ReAttach and assign Alt+Q

Now you can first find your correct process under Alt+E, then attach to it using Alt+W and finally reattach using Alt+Q.

Maybe you want different shortcuts, feel free to do it your way.

Basic ASP.NET MVC Form Validation

Later versions of ASP.NET MVC utilizes jQuery Validate for form validation, and does it well. A few things must be in place for this to work.

I'm going to show you a simple form with a text field and an email address, both of which are required. The email address will also be validated as an email address.

First of all, make sure you use the needed jQuery files. It's important that we use jQuery validate unobtrusive.

The easiest thing here is to install the Nuget package Microsoft.jQuery.Unobtrusive.Validation using Package Manager Console in Visual Studio:

Install-Package Microsoft.jQuery.Unobtrusive.Validation

(Otherwise, you can find the CDN:s on www.asp.net/ajax/cdn)

Don't forget to add the script elements for jQuery and jQuery unobtrusive. It's up to you if you go with the minified versions or the standard versions.

Next up, we will add two properties to our model, preferably a ViewModel.


[Required(ErrorMessage = "*")]
[EmailAddress(ErrorMessage = "*")]
public string Email { get; set; }

[Required(ErrorMessage = "*")]
public string Name { get; set; }

First of all, we declare our properties. We decorate them with the Required and EmailAddress attributes. We need to import the System.ComponentModel.DataAnnotations namespace for this.

As you can see, you can define an error message for both the Required attribute as well as the EmailAddress attribute. I've chosen to just use an asterisk for both now.

Next up, we will render our form:

@using (Html.BeginForm("MyForm", null, FormMethod.Post))
	@Html.LabelFor(x => x.Email, "Email")
	@Html.TextBoxFor(x=>x.EmailTo, new { @placeholder="Email"})
	@Html.ValidationMessageFor(x => x.EmailTo)
	@Html.LabelFor(x => x.Name, "Name")
	@Html.TextBoxFor(x => x.Name, new { @placeholder = "Name" })
	@Html.ValidationMessageFor(x => x.Name)
        <input type="submit" value="Send" />

What is important here is that we use the TextBoxFor and ValidationMessageFor HTML helpers. ValidationMessageFor is what helps us display the error messages we configured in the view model.

Finally, you might want to style the HTML rendered when there's a field with a validation error. There are a few options, styling the input-validation-error css class takes you far.

And this is it! It's all you need to get started with form validation in ASP.NET MVC.

Here are a few useful links if you want to dive deeper into this:

To the top