<|im_start|>user
Write a simple web API in C# that acts like a basic calculator (can perform sum, subtraction and division based on user inputs).<|im_end|>
<|im_start|>assistant
Certainly! Here's an example of a simple web API in C# that acts as a basic calculator and performs addition, subtraction, and division based on user inputs.

```csharp
using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CalculatorAPI
{
    public class CalculatorController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage Add(int a, int b)
        {
            int result = a + b;
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [HttpGet]
        public HttpResponseMessage Subtract(int a, int b)
        {
            int result = a - b;
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [HttpGet]
        public HttpResponseMessage Divide(int a, int b)
        {
            if (b == 0)
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot divide by zero.");

            double result = (double)a / b;
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}
```

This code defines a CalculatorController class that inherits from ApiController in the System.Web.Http namespace. The controller contains three methods: Add, Subtract, and Divide, each decorated with the 'HttpGet' attribute to handle HTTP GET requests.

The Add method takes two integers a and b as parameters, performs addition, and returns the result as an HttpResponseMessage with the HTTP status code 200 (OK).

The Subtract method subtracts b from a and returns the result in a similar way.

The Divide method divides a by b after checking if b is zero. If b is zero, it returns an error response with the HTTP status code 400 (BadRequest). Otherwise, it performs the division and returns the result.

To use this web API, you'll need to host it using a web server or framework, such as ASP.NET or ASP.NET Core, and define appropriate routes for the API endpoints.

Note: This is a simplified example for demonstration purposes. In a production environment, you may want to include input validation and error handling for better reliability and security.<|im_end|>
