Documentation

Ucommerce includes full API reference documentation and lots of helpful articles to help you build your e-commerce site as effortlessly as possible.

Topics Payment Providers
v7.18

Add a new Web Service

Ucommerce uses WebAPI 2 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 CMS's web services.

    
    
    	[RoutePrefix("ucommerceapi/Basket")]
    	public class BasketController : ApiController
    	{
    		[Route("Get")]
    		public IHttpActionResult Get()
    		{
    			if (!TransactionLibrary.HasBasket())
    			{
    				return NotFound();
    			}
    
    			//Use Ucommerce's top level APIs to get current basket for the customer.
    			PurchaseOrder purchaseOrder = TransactionLibrary.GetBasket(false).PurchaseOrder;
    
    			BasketModel basket = ConvertPurchaseOrderToBasketModel(purchaseOrder);
    
    			return Ok(basket);
    		}
    	}
    

Once you have deployed this web service, it will be available on the url "/ucommerceapi/basket/get"

Attributes

"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.

    
    
    [Route("Get")]
    [IsAuthenticated]
    public IHttpActionResult Get()
    {
    

"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.

    
    
    [Route("Get")]
    [IsInRole(typeof(SettingsRole))]
    public IHttpActionResult Get()
    {
    

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.