ASP.NET MVC — web-API

ASP.NET The Web API is an infrastructure that simplifies the creation of HTTP services that cover a wide range of clients, including browsers and mobile devices. The ASP. NET Web API is an ideal platform for creating RESTful applications .NET Framework.

When you create APIs on the web, there are several ways to create APIs on the web. These include HTTP / RPC, and that means using HTTP in a remote procedure call to call things like methods over the internet.

The verbs themselves are included in APIs such as Get Customers, Insert Invoice, Delete Customer, and each of these endpoints ultimately represents a separate URI.

Let’s look at a simple example of a web API by creating a new web application ASP.NET.

Step 1-Open Visual Studio and select “File” → ” New ” → “Project” menu item.

A new project dialog box opens.

Step 2-In the left pane, select Templates → Visual C # → The Internet.

Step 3-In the middle panel, select the Web application ASP.NET

Enter the WebAPIDemo project name in the Name field and click Ok to continue. You will see the following dialog box asking you to set the initial content for the project ASP.NET.

Step 4. To simplify the task, select the “Clear” option and select the “Web API” checkbox in the “Add folders and main links for” section and click OK.

Step 5-It will create a basic MVC project with minimal predefined content.

After creating a project in Visual Studio, you will see several files and folders displayed in the Solution Explorer window.

Step 6-Now we need to add a model. Right-click the Models folder in the Solution Explorer and select Add → Class.

You will now see the Add New Item dialog box.

Step 7-Select Class in the middle panel and enter Employee. cs in the name field.

Step 8-Add some properties to the Employee class using the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebAPIDemo.Models {
   public class Employee{
      public int ID { get; set; }
      public string Name { get; set; }
      public DateTime JoiningDate { get; set; }
      public int Age { get; set; }
   }
}

Step 9-Let’s add a controller. Right-click the controller folder in the Solution Explorer and select Add – > Controller.

The Add Scaffold dialog box appears.

Step 10-Select the Web API Controller 2-Empty option. This template will create an Index method with the default action for the controller.

Step 11-Click the “Add” button, the “Add Controller” dialog box will appear.

Step 12-Set the name EmployeesController and click”Add”.

You will see a new C # file ” EmployeeController. cs “in the” Controllers ” folder, which is open for editing in Visual Studio with some default actions.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web.Http;
using WebAPIDemo.Models;

namespace WebAPIDemo.Controllers{
   public class EmployeesController : ApiController{
      Employee[] employees = new Employee[]{
         new Employee { ID = 1, Name = "Mark", JoiningDate =
            DateTime.Parse(DateTime.Today.ToString()), Age = 30 },
         new Employee { ID = 2, Name = "Allan", JoiningDate =
            DateTime.Parse(DateTime.Today.ToString()), Age = 35 },
         new Employee { ID = 3, Name = "Johny", JoiningDate =
            DateTime.Parse(DateTime.Today.ToString()), Age = 21 }
      };
		
      public IEnumerable<Employee> GetAllEmployees(){
         return employees;
      }
		
      public IHttpActionResult GetEmployee(int id){
         var employee = employees.FirstOrDefault((p) => p.ID == id);
         if (employee == null){
            return NotFound();
         }
         return Ok(employee);
      }
   }
}

Step 13-Launch this app and specify / api / employee / at the end of the URL and press Enter. You will see the following output.

Step 14. Let us specify the following URL http: / / localhost: 63457 / api / employee / 1, and you will see the following result.

Leave a Reply

Your email address will not be published.

You May Also Like
Read More

App Model ASP.NET

Difference between applications ASP.NET and feature-rich client applications, which makes a lot of sense when analyzing the execution…