Skip to content

Webapi articles

Comma-separated parameter values in WebAPI  

The model binding mechanism in ASP.NET is pretty slick – it’s clever and highly extensible and built on TypeDescriptor system for all sorts of re-use that lets you get out of having to write boilerplate code to map between CLR objects and their web representations.

One surprising thing however is that out of the box neither WebAPI or MVC support comma-separated parameter values when bound to an array, e.g.

public class MyController : Controller {
    public string Page([FromUri]int[] ids) {
        return String.Join(" ; ", ids);
    }
}

Will only return 1 ; 2 ; 3 when supplied with /my/page?ids=1&ids=2&ids=3 and if you instead give it /my/page?ids=1,2,3 it will fail.

The reason for this was likely because there isn’t a standard for this at all and that the former – supported – scenario maps to what forms do when they post multiple value selections such as that in a select list box. The latter however is much more readable and is expected by some client frameworks and supported by some other web frameworks such as the Java Spring MVC framework.

Of course that extensible system lets us easily extend this behavior so that we can support both transparently – and interestingly enough – even mix-and-match on the same URL. So for example;

/my/page?ids=1,2&ids=3 will now return 1 ; 2 ; 3 in our example.

Although this supports both types if you are currently passing invalid or blank values to your app they are treated by the default model binder as 0. e.g. ?ids=&ids=1&ids=a would have been received as 0, 1, 0 but now empty values are skipped and invalid values would throw an error – as they should!

CommaSeparatedArrayModelBinder class

The source is available in the DamienGKit project but also here.

Out of the box it supports integer types and Guid’s although you could extend it to floats and decimals – again just be careful with that formatting!

public class CommaSeparatedArrayModelBinder : IModelBinder {
  private static readonly Type[] supportedElementTypes = {
    typeof(int), typeof(long), typeof(short), typeof(byte),
    typeof(uint), typeof(ulong), typeof(ushort), typeof(Guid)
  };

  public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) {
    if (!IsSupportedModelType(bindingContext.ModelType)) return false;
    var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    var stringArray = valueProviderResult?.AttemptedValue?
                      .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    if (stringArray == null) return false;
    var elementType = bindingContext.ModelType.GetElementType();
    if (elementType == null) return false;

    bindingContext.Model = CopyAndConvertArray(stringArray, elementType);
    return true;
  }

  private static Array CopyAndConvertArray(IReadOnlyList sourceArray, Type elementType) {
    var targetArray = Array.CreateInstance(elementType, sourceArray.Count);
    if (sourceArray.Count > 0) {
      var converter = TypeDescriptor.GetConverter(elementType);
      for (var i = 0; i < sourceArray.Count; i++)
        targetArray.SetValue(converter.ConvertFromString(sourceArray[i]), i);
    }
    return targetArray;
  }

  internal static bool IsSupportedModelType(Type modelType) {
    return modelType.IsArray && modelType.GetArrayRank() == 1
      && modelType.HasElementType
      && supportedElementTypes.Contains(modelType.GetElementType());
  }
}

public class CommaSeparatedArrayModelBinderProvider : ModelBinderProvider {
  public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType) {
    return CommaSeparatedArrayModelBinder.IsSupportedModelType(modelType)
      ? new CommaSeparatedArrayModelBinder() : null;
  }
}

To register

It’s necessary to register ModelBinderProviders with your ASP.NET application at start-up, usually in the WebApiConfig.cs file.

public static class WebApiConfig {
  public static void Register(HttpConfiguration config) {
    // All your usual configuration up here
    config.Services.Insert(typeof(ModelBinderProvider), 0, new CommaSeparatedArrayModelBinderProvider());
  }
}

[)amien

Differences between Azure Functions v1 and v2 in C#  

I’ve been messing around in the .NET ecosystem again and have jumped back in with Azure Functions (similar to AWS Lambda) to get my blog onto 99% static hosting. I immediately ran into the API changes between v1 and v2 (currently in beta).

These changes are because v1 was based around .NET 4.6 using WebAPI 2 while the v2 is based on ASP.NET Core which uses MVC 6. There are some guides around to converting but none in the pure context of Azure Functions.

I’ll illustrate with a PageViewCount sample that uses Table Storage to retrieve and update a simple page count.

v1 (.NET 4.61 / WebAPI 2)

[FunctionName("PageView")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get")]HttpRequestMessage req, TraceWriter log)
{
    var page = req.MessageUri.ParseQueryString()["page"];
    if (String.IsNullOrEmpty(page))
        return req.CreateErrorResponse(HttpStatusCode.BadRequest, "'page' parameter missing.");

    var table = Helpers.GetTableReference("PageViewCounts");
    var pageView = await table.RetrieveAsync<PageViewCount>("damieng.com", page) ?? new PageViewCount(page) { ViewCount = 0 };
    var operation = pageView.ViewCount == 0
        ? TableOperation.Insert(pageView)
        : TableOperation.Replace(pageView);

    pageView.ViewCount++;
    await table.ExecuteAsync(operation);

    return req.CreateResponse(HttpStatusCode.OK, new { viewCount = pageView.ViewCount });
}

v2 (ASP.NET Core / MVC 6)

[FunctionName("PageView")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get")]HttpRequest req, TraceWriter log)
{
    var page = req.Query["page"];
    if (String.IsNullOrEmpty(page))
       return new BadRequestObjectResult("'page' parameter missing.");

    var table = Helpers.GetTableReference("PageViewCounts");
    var pageView = await table.RetrieveAsync<PageViewCount>("damieng.com", page) ?? new PageViewCount(page) { ViewCount = 0 };
    var operation = pageView.ViewCount == 0
        ? TableOperation.Insert(pageView)
        : TableOperation.Replace(pageView);

    pageView.ViewCount++;
    await table.ExecuteAsync(operation);

    return new OkObjectResult(new { viewCount = pageView.ViewCount });
}

Differences

The main differences are that:

  1. The return types are now the simpler IActionResult/ObjectResult objects rather than extension methods off of HttpRequestMessage (so, easier to mock/create custom ones)
  2. The input is the HttpRequest object rather than HttpResponseMessage (easier to get query parameters)

If you get an error about ‘Can not create abstract class’ when executing your function then you are trying to use the wrong one in the wrong environment!

Helpers

Both classes above use a small helper class to take care of Table Storage which doesn’t have the nicest to use API. A wrapper much like a data context that ensures the right types go to the right table names might be an even better options.

static class Helpers
{
    public static CloudStorageAccount GetCloudStorageAccount()
    {
        var connection = ConfigurationManager.AppSettings["DamienGTableStorage"];
        return connection == null 
            ? CloudStorageAccount.DevelopmentStorageAccount
            : CloudStorageAccount.Parse(connection);
    }

    public static CloudTable GetTableReference(string name)
    {
        return GetCloudStorageAccount().CreateCloudTableClient().GetTableReference(name);
    }

    public static async Task<T> RetrieveAsync<T>(this CloudTable cloudTable, string partitionKey, string rowKey) 
       where T:TableEntity
    {
        var tableResult = await cloudTable.ExecuteAsync(TableOperation.Retrieve(partitionKey, rowKey));
        return (T)tableResult.Result;
    }
}

To compile

If you want to compile this or maybe you were just looking for code to do a simple page counter here’s the missing TableEntity class;

public class PageViewCount : TableEntity
{
    public PageViewCount(string pageName)
    {
        PartitionKey = "damieng.com";
        RowKey = pageName;
    }

    public PageViewCount() { }
    public int ViewCount { get; set; }
}

[)amien