Basic Web API with ASP.NET 5

I am going to create basic web API access to the contacts data I have been using in previous posts. To start with I added an API folder to my project to hold my API controller. Next I added a contacts controller to the API folder by right clicking on the folder and selecting add new item.

AddNewItem

From the add new item under DNX selected Web API Controller Class, entered a name and clicked add.

AddNewItemDialog

From the resulting code I removed all the actions except for two get functions.

The above code contains a lot of new concepts I am going to break it down more.

The first thing to notice is the route attribute on the class declaration. The route attribute is how the routing engine determines where to send requests. Using [controller] tells the routing engine to use the class name minus the word controller. For example the above route handles api/contacts.

The constructor takes the DbContext needed to access contacts. Note that the context is being automatically injected via the constructor thanks to the fact that ASP.NET 5 now comes with dependency injection out of the box.

First get function returns all contacts and the second returns a specific contact based on the contact’s ID.

Note that the query contains three includes and each of the included classes contain a navigation property back to the main contact. For example here is the email address model.

All of the above compiles and seems to run fine, but will not provide a response. The navigation property for contact creates a circular reference that the response serializer throws an exception trying to serialize.

Thankfully the framework has a configuration option to work around this problem. In the ConfigureServices function of the Startup class add the following.

The above options marks the Contact property as a reference and does not try to circularly serialize it.

Now by running the project and going to http://localhost:port/api/contacts/1 in the browser I get all the contact data related to the contact with an ID of 1. I recommend using something like Postman to make the result more readable if you don’t have a front end to display the data.

12 thoughts on “Basic Web API with ASP.NET 5”

    1. Jerrie, ConfigureMvcJson() was replaced in beta 7. Try the following.

      services.AddMvc()
      .AddJsonOptions(options =>
      {
      options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.All;
      });

      1. Wonderful thanks!

        This was my hack before:

        services.Configure(options =>
        {
        var jsonFormatter = options.OutputFormatters.FirstOrDefault(formatter => formatter is JsonOutputFormatter) as JsonOutputFormatter;
        if (jsonFormatter != null)
        {
        var settings = jsonFormatter.SerializerSettings;
        settings.Formatting = Formatting.Indented;
        settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
        settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        settings.NullValueHandling = NullValueHandling.Ignore;
        }

        jsonFormatter = options.InputFormatters.FirstOrDefault(formatter => formatter is JsonOutputFormatter) as JsonOutputFormatter;
        if (jsonFormatter != null)
        {
        var settings = jsonFormatter.SerializerSettings;
        settings.Formatting = Formatting.Indented;
        settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
        settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        settings.NullValueHandling = NullValueHandling.Ignore;
        }
        });

        And after your help, it now looks like this:

        services.AddMvc()
        .AddJsonOptions(options =>
        {
        var settings = options.SerializerSettings;
        settings.Formatting = Formatting.Indented;
        settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
        settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        settings.NullValueHandling = NullValueHandling.Ignore;
        });

      1. Hi. I’m sorry, but it seems my comment was not correct either. I’m pretty sure I intended to write “return type should be Task” but for some reason it looks like I wrote only Task. Apologies.

Leave a Reply

Your email address will not be published. Required fields are marked *