Generated Web API

Web API


Listed below are quick descriptions of the Web API controller class files AspCoreGen 2.0 Razor generates. These Web API methods encapsulates calls to the Middle Layer methods. Generating Web APIs are optional. Web APIs can be a client and act as another middle layer. Generated Web API methods can be accessed publicly and can act as a service to other clients (winforms, web forms, other api or service like a wcf app, mobile app, etc). So you can also build a desktop app that can access the same API your ASP.Net Core app is using through the Web API. Clients/consumers of the Web API does not have to be a windows program, it returns collections as JSON data, or you can pass it JSON data when saving a new record or updating an existing record.

Web APIs are generated as a separate ASP.NET Core project in the same solution as the generated web application.

One of each of the following objects listed below is generated per table in your database.

Generated Class (.cs) Quick Description (WebApi Folder)
1. Web API Controller Base Class

  - Sample Code

An API controller class file containing business methods. The methods encapsulates calls to the middle tier methods. Used as a base class.
2. Web API Controller Class

  - Sample Code

An API controller class file derived from the web api controller base class. Additional methods may be added here.



Consuming Web API Code Examples
The generated Web APIs can be consumed by various clients as mentioned above. Here are some code examples on how a windows-based client such as an MVC web app, web forms, win forms, web service (.asmx, wcf), etc can access the API. The examples below shows how to consume/call the generated Web API.

Note 1: Examples below shows Microsoft C# examples. Because these are Web APIs it can also be accessed by other programming language like PHP, or Java, etc.

Note 2: We made it even easier by generating all the code examples for each of the method that you can access. More operations can be consumed as shown here, and these operations are generated for each of your database table.


Select/Skip/Take: E.g. Sort Products in descending order, Skip the first 20 records and then Select 10 Product records. This will return a strongly-typed ProductsCollection collection. You can then just assign the returned collection to a GridView (web forms), or use a loop through the collection using foreach.

List<Products> objProductsCol = null;
 
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(Functions.GetWebApiBaseAddress());
    bool isforJqGrid = false;
    HttpResponseMessage response = 
         client.GetAsync("ProductsApi/SelectSkipAndTake/?rows=" + rows + 
         "&startRowIndex=" + startRowIndex + "&_page=" + currentPage + 
         "&sidx=" + sidx + "&sord=" + sord + 
         "&isforJqGrid=" + isforJqGrid).Result;
 
    if (!response.IsSuccessStatusCode)
    {
        throw new Exception("Error Status Code: " + response.StatusCode.ToString() + 
            " Error Reason: " + response.ReasonPhrase + 
            " Error Message: " + response.RequestMessage.ToString());
    }
    else
    {
        var responseBody = response.Content.ReadAsStringAsync().Result;
        objProductsCol = JsonConvert.DeserializeObject<List<Products>>(responseBody);
    }
}


Select/Skip/Take Via URL: Returns a collection in JSON format.

http://localhost:27229/ProductsApi/SelectSkipTake/?rows=10&startRowIndex=20&sortByExpression=ProductID desc



Add a New Record: E.g. Pass a JSON formatted data to Add a New Record.

string serializedModel = "{" +
    "\"ProductID\":0," +
    "\"ProductName\":\"My Product\"," +
    "\"SupplierID\":18," +
    "\"CategoryID\":1," +
    "\"QuantityPerUnit\":\"10 per package\"," +
    "\"UnitPrice\":5.00," +
    "\"UnitPriceTotal\":0.0," +
    "\"UnitsInStock\":20," +
    "\"UnitsOnOrder\":12," +
    "\"ReorderLevel\":10," +
    "\"Discontinued\":false" + 
    "}";
 
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(Functions.GetWebApiBaseAddress());
    HttpResponseMessage response = client.PostAsync("ProductsApi/Insert", 
        new StringContent(serializedModel, Encoding.UTF8, "application/json")).Result;
 
    if (!response.IsSuccessStatusCode)
    {
        throw new Exception("Error Status Code: " + 
            response.StatusCode.ToString() + " Error Reason: " + 
            response.ReasonPhrase + " Error Message: " + response.RequestMessage.ToString());
    }
}



Delete a Record by Primary Key:

using (var client = new HttpClient())
{
    int id = 10;
    client.BaseAddress = new Uri(Functions.GetWebApiBaseAddress());
    HttpResponseMessage response = client.DeleteAsync("ProductsApi/Delete/" + id).Result;
 
    if (!response.IsSuccessStatusCode)
        throw new Exception("Error Status Code: " + response.StatusCode.ToString() + 
            " Error Reason: " + response.ReasonPhrase + 
            " Error Message: " + response.RequestMessage.ToString());
}
* Please see notes.