Add a new Web Service
Ucommerce uses WebAPI to host web services and in this article you will learn to create custom web services and how to deploy them with Ucommerce.
How to create a custom web service using WebAPI 2
web services are wired up using attribute routing, you can read more about attribute routing here: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
In this example we are creating a custom web service to get the customers basket from Ucommerce. All web services should start with "ucommerceapi" to avoid conflicts with the CMSs web services.
[System.Web.Mvc.RoutePrefix("ucommerceapi/Basket")] public class BasketController : ApiController { [System.Web.Mvc.Route("Get")] public IHttpActionResult Get() { Ucommerce.Api.ITransactionLibrary transactionLibrary = ObjectFactory.Instance.Resolve<Ucommerce.Api.ITransactionLibrary>(); if (!transactionLibrary.HasBasket()) { return NotFound(); } //Use Ucommerce's top level APIs to get current basket for the customer. Ucommerce.EntitiesV2.PurchaseOrder purchaseOrder = transactionLibrary.GetBasket(false); BasketModel basket = new BasketModel(purchaseOrder); return Ok(basket); } }
Once you have deployed this web service, it will be available on the url "/ucommerceapi/basket/get"
Attributes
[Route("Get")] [IsAuthenticated] [Ucommerce.SystemHttp.Attributes.IsInRole(typeof(Ucommerce.EntitiesV2.SettingsRole))] public class BackOfficeController : ApiController { }
"IsAuthenticated" Attribute
The "IsAuthenticated" ensures that the web services can only be request by someone who is logged in to the back office of the CMS.
"IsInRole" Attribute
The "IsInRole" ensures that the web services can only be request by someone who is logged in to the back office of the CMS and the specified permissions.
All of the roles can be found in the "Ucommerce.EntitiesV2" namespace.
Deployment
The only thing left is deploying your web services to your Ucommerce environment. This is done by adding the assembly to a suitable folder underneath Ucommerce/apps then Ucommerce will automatically pick it up when the application start.