Creating & Deploying WebAPI over CownPeak
Posted by: Dhirendra Singh
The Web API is an extensible framework for building HTTP based services that can be accessed from different platforms like web, windows, mobile and so on. It sends data as a response instead of the html view. This is like a WebService or WCF Service with an exception that it only supports HTTP/HTTPs protocol.
In the article we’ll create a simple WebAPI and understand how to deploy it over Crownpeak.
Prerequisites:
Understanding of C#, Visual Studio 2015, Crownpeak CMS
Lets create a simple API!
For this demonstration we have used Visual Studio 2015 with .Net Framework 4.5.2.
1. Open Visual Studio and create a new project: File > New > Project
2. Set the project name and location, and hit Ok
3. A screen showing existing/installed templates pops-up for the chosen framework.
Select the Empty Template & for Core references, select the Web API checkbox and hit Ok
This will create a new project with some default files/folders that can be seen in the solution explorer.
4. Add a Product Model Class, in our case this class will hold data about products
Add below code for the Product Model
5. Adding Controller: This class will have business logic for fetching data.Add the below lines of code for the controller:
6. Build and run the project & on the browser address bar update the url to be - /api/product/ or /api/product/[ID]Output of the /api/product/ should appear as per the screenshot below
/api/product/[ID] e.g./api/product/1001
7. Now Publish the Project by right clicking on the project in the solution explorer.
Use file system publishing and mention the destination path for publishing.
8. Open your Crownpeak instance and browse to your Sites root. If bin folder exists, you can upload the published dlls to that folder, else create one and upload the DLLs. When creating a new folder, it might require us to set the site root as an application.
9. Deploy all the uploaded dlls to Dev environment
10. Similarly, upload your global.asax file, and deploy it.
11. Test your API on any browser With the below URL sample format:
[http/https]://domain/api/product or
[http/https]://domain/api/product/[product id]
Few notes to remember:
1. Crownpeak site root should be configured to support .Net framework being used.
2. Your site root should be set as an application, else you might experience an error as shown here