Generated Web API


Listed below are quick descriptions of the Web API controller class files AspCoreGen 3.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 Web API Quick Description
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. Your own custom (code) 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 Razor 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

For example Sort Products in descending order, Skip the first 29 rows/records and then Select 10 Product records. You can then assign the returned List of Products (collection) to a GridView (web forms) or any form of control that accepts a collection as it's data source, or simply loop through it using the foreach statement.

string sidx = "ProductID"// field to sort
string sord = "desc";      // sort order. asc or an empty string = ascending.  desc = descending
int page = 3;              // page to retrieve. page 1 = records 1 to 10, page 3 = records 21 to 30
                           // e.g. 90 records produces 9 pages when retrieving 10 rows per page
int rows = 10;             // number or rows/records to retrieve
 
// get records via the web api
List<Products> objProductsCol = null;
string response = await Functions.HttpClientGetAsync("ProductsApi/SelectSkipAndTake/?rows=" + rows + 
    "&page=" + page + "&sidx=" + sidx + "&sord=" + sord);
 
if(!String.IsNullOrEmpty(response))
    objProductsCol = JsonSerializer.Deserialize<List<Products>>(response);

Select/Skip/Take Via URL

Returns a List of Products (List<Products>) collection in JSON format just like the example shown above.

http://localhost:27229/ProductsApi/SelectSkipTake/?rows=10&page=3&sidx=ProductID&sord=desc

Add a New Record

For example, 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" +
    "}";
 
string webApiUrl = "ProductsApi/Insert";
 
// insert data via web api
HttpResponseMessage response = await Functions.HttpClientPostAsync(webApiUrl, serializedModel);
 
// check the status of the httpclient post operation
if (response != null)
{
    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


int id = 10;
 
// delete data via web api
HttpResponseMessage response = await Functions.HttpClientDeleteAsync("ProductsApi/Delete/" + id);
 
// check the status of the httpclient delete operation
if (response != null)
{
    if (!response.IsSuccessStatusCode)
    {
        return BadRequest(response.ReasonPhrase);
    }
}