Web API and OData controllers

Important

This topic applies to Dynamics 365 for Retail, Dynamics 365 for Finance and Operations, Enterprise edition, and Dynamics 365 for Operations.

This article provides code to extend the ApiController class so that you can create a Web API controller for Retail Server.

By default, all the Retail Server binaries in Microsoft Dynamics 365 for Retail use only OData. If you want to use a controller that uses a traditional Web API, you can create your own Web API controller and extend the Web API configuration. The following example shows how to create a Web API controller for Retail Server.

namespace Microsoft.Dynamics.RetailServer.Samples.Extensions
{
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Web.Http;
    using Commerce.Runtime.DataModel;
    using Retail.StoreServerServiceLibrary;
    [ComVisible(false)]
    [ExtendedController("Values")]
    [CommerceAuthorization(AllowedRetailRoles = new string[] { CommerceRoles.Anonymous }, CheckRetailOperation = false)]
    public class ValuesController : ApiController
    {
        // GET /api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
}

To create the controller, create a new class that uses the Export attribute, and specify that the type is the IWebApiConfig interface. The IWebApiConfig interface has one method that you can override, the Register method. After you override the Register method, you can call the base class to get the same mapping as an OData metadata controller. You must derive from the standard Web API controller. You can then customize the controller to meet your business requirements. For more information, see ASP.NET Web API.