Reusable Web API Solution In ASP.NET

This blog talks about how to design a Web API solution and reuse it in ASP.NET Web Projects.

Scenario

I had a scenario in an ASP.NET Web forms application where the employee details like First Name, Last Name etc., should be populated in the employee number textbox and these textboxes should be disabled after data is populated. 

If the selected employee number doesn't exist in the relevant database then the user should be allowed to key in the details.

This requirement needs to be implemented in multiple .aspx pages.

Solution

As the web application has a common javascript file referenced in the site master page, I have created a common function to get the employee number as input and populate the data using the Web API.

The common function code is as follows,
  1. function GetEmployeeDetails(employeeNo, apiSource) {  
  2.     var apiUrl = "/api/HelpdeskEmployee/GetEmployeeDetails?empNo =" + employeeNo;  
  3.     $.ajax({  
  4.         url: apirUrl,  
  5.         type: "GET",  
  6.         contentType: "application/json;charset=utf-8",  
  7.         dataType: "json",  
  8.         success: function(data) {  
  9.             if (data != null) {  
  10.                 switch (apiSource) {  
  11.                     case "staff":  
  12.                         $("#txtStaffFirstName").val(data.FirstName);  
  13.                         $("#txtStaffMiddleName").val(data.MiddleName);  
  14.                         $("#txtStaffLastName").val(data.LastName);  
  15.                         $("#txtStaffTitle").val(data.Title);  
  16.                         break;  
  17.                     case "security-staff":  
  18.                         $("#txtSecStaffFirstName").val(data.FirstName);  
  19.                         $("#txtSecStaffMiddleName").val(data.MiddleName);  
  20.                         $("#txtSecStaffLastName").val(data.LastName);  
  21.                         break;  
  22.                 } // end switch  
  23.                 ToggleStaffInputs(apiSource);  
  24.                 return false;  
  25.             }  
  26.         },  
  27.         error: function(data) {  
  28.             // exception handling logic goes here....  
  29.         }  
  30.         return false;  
  31.     });  
  32. }  
In the above function, the selected employee number and calling input identifier are passed as parameters. The "apiSource" parameter is use to identify for which section of the aspx page the data needs tobe populated. This is useful when we have multiple employee number textboxes in the same aspx page.

The function "ToggleStaffInputs(apiSource)" is written in the individual aspx page script file to disable the name and title textboxes if data is populated otherwise enable them for user entry.

The code for ToggleStaffInputs function is given below. This is available in the aspx page level .js file.
  1. function ToggleStaffInputs(apiSource) {  
  2.     switch (apiSource) {  
  3.         case "staff":  
  4.             $("#txtStaffFirstName").val() != "" ? $("#txtStaffFirstName").prop("disabled"true) : $("#txtStaffFirstName").prop("disabled"false);  
  5.             $("#txtStaffMiddleName").val() != "" ? $("#txtStaffMiddleName").prop("disabled"true) : $("#txtStaffMiddleName").prop("disabled"false);  
  6.             $("#txtStaffLastName").val() != "" ? $("#txtStaffLastName").prop("disabled"true) : $("#txtStaffLastName").prop("disabled"false);  
  7.             $("#txtStaffTitle").val() != "" ? $("#txtStaffTitle").prop("disabled"true) : $("#txtStaffTitle").prop("disabled"false);  
  8.             break;  
  9.         case "security-staff":  
  10.             $("#txtSecStaffFirstName").val() != "" ? $("#txtSecStaffFirstName").prop("disabled"true) : $("#txtSecStaffFirstName").prop("disabled"false);  
  11.             $("#txtSecStaffMiddleName").val() != "" ? $("#txtSecStaffMiddleName").prop("disabled"true) : $("#txtSecStaffMiddleName").prop("disabled"false);  
  12.             $("#txtSecStaffLastName").val() != "" ? $("#txtSecStaffLastName").prop("disabled"true) : $("#txtSecStaffLastName").prop("disabled"false);  
  13.             break;  
  14.     }  
  15. }  
This function will be executed after the success block to enable/disable the textboxes based on the value.

Next is to write the page level scripts to call the common GetEmployeeDetails function.

This script is given below,
  1. $("#txtStaffEmployeeNo").blur(function() {  
  2.     var staffEmployeeNo = $("#txtStaffEmployeeNo").val();  
  3.     if (staffEmployeeNo.trim() != '') GetEmployeeDetails(staffEmployeeNo.trim(), 'facility-head');  
  4.     else ToggleStaffInputs('staff');  
  5. });  
  6. $("#txtSecStaffEmployeeNo").blur(function() {  
  7.     var securityStaffEmployeeNo = $("#txtSecStaffEmployeeNo").val();  
  8.     if (securityStaffEmployeeNo.trim() != '') GetEmployeeDetails(securityStaffEmployeeNo.trim(), 'facility-head');  
  9.     else ToggleStaffInputs('staff');  
  10. });  
The Web API code is given below.
  1. [HttpGet]  
  2. public HttpResponseMessage GetEmployeeDetails(string employeeNo) {  
  3.     try {  
  4.         var employee = _employeeService.GetEmployeeDetails(employeeNo);  
  5.         HttpResponseMessage httpResponse = null;  
  6.         httpResponse = employee != null ? Request.CreateResponse(HttpStatusCode.OK, employee) : new  
  7.         HttpResponseMessage(HttpStatusCode.NotFound);  
  8.         return httpResponse;  
  9.     } catch (Exception ex) {  
  10.         // exception logic....   
  11.     }  
  12. }  
The Web API returns an object of type Employee which has the properties like First Name, Last Name, Middle Name, Title.

Using this approach, we can reuse the Web API solution throughout the web application.
Ebook Download
View all
Learn
View all