Category: Code


BundleConfig

Bundle virtual-paths should not be duplicated, otherwise the dupe will overwrite the contents of the previous entry. To add more than one file to a bundle, wrap them in the params in the Include method.

For example, in BundleConfig.cs RegisterBundles, this:

bundles.Add(new StyleBundle("~/bundles/css").Include("~/Content/xxx.less"));
bundles.Add(new StyleBundle("~/bundles/css").Include("~/Content/yyy.less"));

Needs to be this:

bundles.Add(new StyleBundle("~/bundles/css").Include(
                    "~/Content/xxx.less",
                    "~/Content/yyy.less"
                ));

Also, the bundle virtual path depth should be the same as the depth of the file you are referring to (4 in the example below). If you don’t do this, relative paths in your less/css will fail.

	bundles.Add(new LessBundle("~/Content/xxx/less/yyy").Include("~/Content/xxx/less/yyy.less"));

How to fix the Visual Studio 2012 skin

The Visual Studio 2012 default skin is just awful. To fix it, here are the shortcuts.

To use this nice dark theme, import this VSSettings file:

Run this powershell command to turn off the ALL CAPS menu (if in Visual Studio full version):

Set-ItemProperty -Path HKCU:\Software\Microsoft\VisualStudio\11.0\General -Name SuppressUppercaseConversion -Type DWord -Value 1

(or if in Visual Studio Express Web):

Set-ItemProperty -Path HKCU:\Software\Microsoft\VWDExpress\11.0\General -Name SuppressUppercaseConversion -Type DWord -Value 1

If you want to run your apps in localhost through IIS Express without having to launch Visual Studio in admin mode every time, run this one-time command through an admin-elevated cmd (replacing your app name and port):

>netsh http add urlacl url=http://localhost.myapp.com:1337/ user=everyone

Now you can launch Visual Studio in normal mode and when you Ctrl-F5, you won’t get a “failed to register url” error.

To automagically pass a select option (a.k.a. DropDownBox or ComboBox) to a post action’s viewmodel using MVC.NET’s built in model binding, simply set the “name” (not the “id”) of the select element to the name of the viewmodel parameter. Make sure the option values are also being populated.  Like so:

@using (Html.BeginForm())
{
    
    
}

[Serializable]
public class MyViewModel
{
    public string ItemId{ get; set; }
    public List Items{ get; set; }
}

ToXmlString extension method

This is an extension method that serializes any object to xml. Note that it is “null resistant” in that if the target is null, the extension does not throw an object-not-set-to-an-instance exception, but instead simply returns null.

public static string ToXmlString(this T item) where T : class
        {
            if (item == null)
            {
                return null;
            }
            string xml;
            using (var stream = new MemoryStream())
            using (var writer = XmlWriter.Create(stream))
            {
                new XmlSerializer(item.GetType()).Serialize(writer, item);
                xml = Encoding.UTF8.GetString(stream.ToArray());
            }
            return xml;
        }
© 2013 Robert Corvus