This section will describe how to work with Web Api in Umbraco to easily create REST services
Related links:
The Microsoft Web API reference can be found here - "ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework."
Essentially it's a great platform for building REST services.
We've created a base Web Api controller for developers to inherit from which will expose all of the Umbraco related services and objects that you will require when working with Umbraco.
The class to inherit from is: Umbraco.Web.WebApi.UmbracoApiController
This will expose the following properties for you to use:
ApplicationContext ApplicationContext {get;}
ServiceContext Services {get;}
DatabaseContext DatabaseContext {get;}
UmbracoHelper Umbraco {get;}
UmbracoContext UmbracoContext {get;}
There are 2 types of Umbraco Api controllers:
- A locally declared controller - it is not routed via an Area
- A plugin based controller - it is routed via an Area
When working on your own projects you will normally be creating a locally declared controller which requires no additional steps. However, if you are creating an Umbraco package to be distributed you will want to create a plugin based controller so that it gets routed via its own area. This ensures that the route will not overlap with someone's locally declared controller if they are both named the same thing.
It is very important that you name your controllers according to these guidelines or else they will not get routed:
All controller class names must be suffixed with "Controller" and inherit from UmbracoApiController. Some examples:
public class ProductsController : UmbracoApiController
public class CustomersController : UmbracoApiController
public class ScoresController : UmbracoApiController
This is the most common way to create an Umbraco Api controller, you simply inherit from the class Umbraco.Web.WebApi.UmbracoApiController
and that is all. You will of course need to follow the guidelines specified by Microsoft for creating a Web Api controller, documentation can be found here.
Example:
public class ProductsController : UmbracoApiController
{
public IEnumerable<string> GetAllProducts()
{
return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
}
}
All locally declared Umbraco api controllers will be routed under the url path of:
~/Umbraco/Api/[YourControllerName]
E.g ~/Umbraco/Api/Products/GetAllProducts
If you are creating an Umbraco Api controller to be shipped in an Umbraco package you will need to add the Umbraco.Web.Mvc.PluginController
attribute to your controller to ensure that it is routed via an area. The area name is up to you to specify in the attribute.
Example:
[PluginController("AwesomeProducts")]
public class ProductsController : UmbracoApiController
{
public IEnumerable<string> GetAllProducts()
{
return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
}
}
Now this controller will be routed via the area called "AwesomeProducts". All plugin based Umbraco api controllers will be routed under the url path of:
~/Umbraco/[YourAreaName]/[YourControllerName]
For more information about areas, Urls and routing see the routing section
If you are creating a controller to work within the Umbraco back office then you will need to ensure that it is secured properly by inheriting from: UmbracoAuthorizedApiController
or UmbracoAuthorizedJsonController
. This controller type will auto-route your controller like the above examples except that it will add another Uri path: 'backoffice'. For example:
~/Umbraco/backoffice/Api/[YourControllerName] ~/Umbraco/backoffice/[YourAreaName]/[YourControllerName]