Monthly Archives: September 2011

Setting Content-Type in an Umbraco /Base service

The Umbraco /Base system is great for creating simple REST-like services. You can use the RestExtensionMethod attribute to instruct Umbraco to return your response as XML or not. But what is “not”? Apparently it means set Response.ContentType to “text/html”. But what if the returned data should be sent using a different mime type? Setting the ContentType manually like

HttpContext.Current.Response.ContentType = "application/json"

will not do the trick. Umbraco /Base will override it with “text/html” if you are returning a string from your service method.

What you need to do is to change your method return signature to “void” and write your data directly to the HttpResponse, like:

HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write(stringWriter.ToString());

Umbraco will not override the content type for “void”-methods. Below is a full example.

[RestExtension("Member")]
public class MemberService
{
	[RestExtensionMethod(returnXml = false, allowAll = true)]
	public static void Json()
	{
		var json = "{ \"firstName\": \"John\", \"lastName\": \"Smith\", \"age\": 25 }";

		// Write response and set content type
		HttpContext.Current.Response.ContentType = "application/json";
		HttpContext.Current.Response.Write(json);
	}
}

ILSpy plugin for Visual Studio

ILSpy is an open-source .NET assembly browser and decompiler like Red Gates Reflector which recently dropped their free version. ILSpy is very easy to use and can decompile a .NET assembly to IL or C# code. I often use it to learn more of the internals of the .NET framework or a third party plugin/framework. However I found it tedious to locate the assembly I wanted to decompile in Windows Explorer and open it in ILSpy. Then I came across Eric Carr’s Visual Studio plugin for ILSpy. It creates a “Browse source”-shortcut in the “Tools” menu of Visual Studio and opens the currently highlighted class, property, or method in ILSpy.

I have encouraged Eric Carr to release the plugin as open source, but at the moment you can download a MSI installer of the plugin.