REST Resource Quick Start

Next: Hello World

Let’s get a quick app up an running using REST Resource. While REST Resource is agnostic to the technology used for creating a web service, we’ll create an ASP.NET Core MVC web service. I am going to assume you know you to create a Core MVC application, but we’ll need to make a few additions to wire everything up. The first step is we’ll need to add a few formatters so that our controllers know how to transform our Resource objects into something that client applications can consume. You’ll need the Slysoft REST Resource ASP.NET Core Utils package for this. This package will also import the following packages

  • Slysoft REST Resource
  • Slysoft REST Resource HAL+JSON Utils
  • Slysoft REST Resource HAL+XML Utils
  • Slysoft REST Resource HTML Utils

The first of this is the core package containing a Resource class and some extension methods for interacting with it, while the last three packages contain logic for transforming JSON/XML/HTML to/from (only “from” in the case of HTML) a Resource object.

After adding the package to your project, add the following lines into your program startup code:

builder.Services.AddControllers(options => {
    options.OutputFormatters.Insert(0, new ResourceHalJsonFormatter());
    options.OutputFormatters.Insert(1, new ResourceHalXmlFormatter());
    options.OutputFormatters.Insert(2, new ResourceHtmlFormatter());
    options.RespectBrowserAcceptHeader = true;
});

This will enable you to create a Resource object, return it from a controller, and it will be transformed into JSON, XML, or HTML depending on the Accept header of the request. So let’s return something! Next up is the requisite Hello World application.

Blog at WordPress.com.