Select version
v7.13 v7.12 v7.11 v7.9 v7.8 v7.7 v7.6 v7.5 v7.3 v7.1 v7.0 v6.8 v6.7 v6.6 v6 v5 v4 v3 v2

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.

Ucommerce Aps - Vestergade 58N, 4th. floor - 8000 Aarhus C - Denmark
Phone +45 61 799 997 - Email [email protected] Ucommerce logo