Visual Studio: Unable to start debugging on the web server

This happened to me today – I was getting the same error whenever I tried to debug an ASP.NET application using Visual Studio:

Unable to start debugging on the web server. Could not start ASP.NET debugging. More information may be available by starting the project without debugging.

debug1

My initial reaction was to check if there was something wrong in IIS, and I was right: the application pool used by the application I wanted to debug was stopped!

debug2

At that moment I realised that I changed my Windows password 2 or 3 hours before trying to debug the application. Given that the application pool was running under my credentials, all I had to do to fix the issue was to right-click the application pool and go to Advanced Settings > Identity and update my password 🙂

 

ASP.NET MVC: Creating localized DropDownLists for enums

A collection of HTML helpers that generate DropDownlists for enums, with or without localization support.

Table of contents

 

HTML Helpers Overview

I’ve created a set of HTML helpers that generate a DropDownList for an enum.
Those helpers are similar to DropDownList Method and DropDownListFor Method, with the only difference being that the those helpers will populate the DropDownList with the elements of the specified enum.

 

Some examples – Basic usage

Let’s assume the following model:

public enum WeekDay
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

public class WeeklyEvent
{
    public string Title { get; set; }
    public WeekDay Day { get; set; }
    public WeekDay? AnotherDay { get; set; }
}

 

Setting the name of the element and default empty item text

This is the most basic usage, the generated text will be enumValue.ToString():

// name="eventDay"
@Html.EnumDropDownList<WeekDay>("eventDay", "Select an item")

// name="Day"
@Html.EnumDropDownListFor<WeeklyEvent, WeekDay>(x => x.Day, "Select an item")

Html.EnumDropDownListFor works with nullables too:

@Html.EnumDropDownListFor<WeeklyEvent, WeekDay?>(x => x.AnotherDay, "Select an item")

 

Using [Description] attribute

You can customize the text using DescriptionAttribute:

public enum WeekDay
{
    [Description("Domingo")]
    Sunday,

    [Description("Segunda")]
    Monday,

    [Description("Terça")]
    Tuesday,

    [Description("Quarta")]
    Wednesday,

    [Description("Quinta")]
    Thursday,

    [Description("Sexta")]
    Friday,

    [Description("Sábado")]
    Saturday
}

 

Using custom HTML attributes

Just like DropDownList and DropDownListFor, you can use custom HTML attributes

@Html.EnumDropDownList<WeekDay>("eventDay", "Select an item", new { @class="select"})

@Html.EnumDropDownListFor<WeeklyEvent, WeekDay>(x => x.Day, "Select an item" , new { @class="select"})

Continue reading

ASP.NET MVC Localization: Generate resource files and localized views using custom templates

Use ASP.NET MVC T4 custom templates to generate resource files and localized views.

Table of contents

Overriding ASP.NET MVC custom templates

The Add View dialog perform code generation that use T4 templates behind the scenes. These templates can be modified to customize the generated code from these tools. You can also add custom templates.

Basically you have to copy the default templates to your project. This is the path:

[Visual Studio Install Directory]\Common7\IDE\ItemTemplates\[CSharp | VisualBasic]\Web\MVC\CodeTemplates\

I have created a custom template that generates resource files (.resx), and I have modified the default templates to use the generated resource files.

Views Templates

You can find more information here:

T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers

Understanding .resx file format

From MSDN [1]:

The .resx resource file format consists of XML entries, which specify objects and strings inside XML tags. One advantage of a .resx file is that when opened with a text editor (such as Notepad or Microsoft Word) it can be written to, parsed, and manipulated.(…) Each entry is described as a name/value pair.(…) When a string is added to a .resx file, the name of the string is embedded in a <data> tag, and the value is enclosed in a <value> tag

For a resource file like this:

You have the corresponding XML:

I created a custom template that generates the resource file in the specified format.

Continue reading