In this tutorial, you will learn how to create a very simple JSON API using Umbraco 8. you will learn a simple way to return data. You will also learn about to determine the APIs URL. Returning JSON is the pretty much the standard way of returning data from an API. It is no-surprised then that Umbraco comes with helpers to enabled this functionality. If you want to use Umbraco as a headless CMS then this is how your head will access all the content from Umbraco.
How To Create An API Within Umbraco
To create a JSON API you will need to use two main components:
The good thing about inheriting from the Umbraco API controller is that you will not have to worry about configuring any routing up. Umbraco will handle this for you. The easiest way to return JSON is to use the MVC action result, (https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.jsonresult?view=aspnet-mvc-5.2)[JsonResult]. If you use Umbraco with Models builder if you want to be really lazy you can perform the data look-up queries and then pass those models back. Assuming you had this class:
You could then return:
What's My Umbraco API Url?
Determining the API Url is simple. The URL can be determined by breaking the segments down like this:
In the example above the APIs URL will be:
Where 'MyApi' is the name of the controller and 'GetJson' is the name of the method. One thing to keep in mind is that you will need to start the name of the action with 'Get' otherwise you may not be able to access it. If you want to learn more about web API within Umbraco, you can learn more (https://our.umbraco.org/documentation/reference/routing/webapi/)[here]