Monday 11 January 2016

Using export configuration files in Orchard Import / Export

In the more recent versions of Orchard (I think it came in in 1.9) the export options available at Import/Export became much more powerful. One of the underused features (thanks to a lack of documentation) is "Upload a configuration file with recipe steps to execute" option.


With this option selected, you can upload an xml file that defines which content types and other custom export features you wish to export with your site, allowing you a repeatable process, and avoiding manual mistakes.

Here is an example of such a file.



Note that "PagewithPromotion" is a custom content type that has been added to the site in this example. Unfortunately currently, there is only "inclusive" syntax it appears for listing which content types to export. I'm going to raise an issue for adding "exclude" syntax so that it is easier to say "all content types except bob".

I've constructed this template by looking through the source code. The example works, but there are probably also options I've not spotted, so feel free to explore the code further!

Thursday 7 January 2016

Serializing Enums to String in MVC6

Often you will want to change the default serialization of your API responses, to ensure that enums are reflected as strings, not integers. Why? Well, which is more self documenting?

{
  "Status": 1,
  "Message": "things are on fire"
}

or
{
  "Status": "Error",
  "Message": "things are on fire"
}

Fortunately this is really easy in MVC6. The default code uses the ever populate JSON.NET, and it's options are easily exposed. Note that this code example is against RC1, so should actually be correct going forward (the syntax has changed many times across vnext). I'm also assuming you have created your site using the template application, rather than creating an empty ASP.NET application.

Inside the "ConfigureServices" method find

services.AddMvc();

and replace it with

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.Converters.Add(new StringEnumConverter());
});

As you can see, you should be able to get at all the other json.net options from here too.