Generated Web API


Listed below are quick descriptions of the Web API controller class files AspxFormsGen MVC 5 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 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.

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

Generated Web API Quick Description (WebApi Folder)
1. Web API Controller Base Class 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 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#/VB.NET 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 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 grid/table, or use a loop through the collection using foreach.

C#
List<Products> objProductsCol = null;

using (var client = new HttpClient())
{
  client.BaseAddress = new Uri(Functions.GetWebApiBaseAddress());

  HttpResponseMessage response = 
    client.GetAsync("ProductsApi/SelectSkipAndTake/?rows=10" +
    "&startRowIndex=20&sortByExpression=ProductID desc/").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);
  }
}
VB.NET
Dim objProductsCol As ProductsCollection = Nothing
 
Using client = New HttpClient()
  client.BaseAddress = New Uri(ConfigurationManager.AppSettings("WebApiBaseAddress"))
  Dim response As HttpResponseMessage = client.GetAsync("api/ProductsApi/SelectSkipAndTake/?rows=10" & _
     "&startRowIndex=20&sortByExpression=ProductID desc").Result
 
  If Not response.IsSuccessStatusCode Then
      Throw New Exception("Error Status Code: " & response.StatusCode.ToString() & _
          " Error Reason: " & response.ReasonPhrase & _
          " Error Message: " & response.RequestMessage.ToString())
  Else
      Dim responseBody = response.Content.ReadAsStringAsync().Result
      objProductsCol = JsonConvert.DeserializeObject(Of ProductsCollection)(responseBody)
  End If
End Using

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

For example, pass a JSON formatted data to Add a New Record.

C#
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());
    }
}
VB.NET
Dim serializedModel As String = "{" & _
    """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 client = New HttpClient()
    client.BaseAddress = New Uri(ConfigurationManager.AppSettings("WebApiBaseAddress"))
    Dim response As HttpResponseMessage = client.PostAsync("api/ProductsApi/Insert", _
        New StringContent(serializedModel, Encoding.UTF8, "application/json")).Result
 
    If Not response.IsSuccessStatusCode Then
        Throw New Exception("Error Status Code: " & response.StatusCode.ToString() & _
            " Error Reason: " & response.ReasonPhrase & _
            " Error Message: " & response.RequestMessage.ToString())
    End If
End Using

Delete a Record by Primary Key


C#
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());
}
VB.NET
Using client = New HttpClient()
    Dim id As Integer = 10
    client.BaseAddress = New Uri(ConfigurationManager.AppSettings("WebApiBaseAddress"))
    Dim response As HttpResponseMessage = client.DeleteAsync("api/ProductsApi/Delete/" & id).Result
 
    If Not response.IsSuccessStatusCode Then
        Throw New Exception("Error Status Code: " & response.StatusCode.ToString() & _
            " Error Reason: " & response.ReasonPhrase & _
            " Error Message: " & response.RequestMessage.ToString())
    End If
End Using