Hi rani,
Refer below code.
First create a WebAPI project with ASP.Net Core template.
In teh Models folder create a class Customer and add properties to it.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Then add controller to it and write the below code in the controller.
[Route("api/CustomerAPI/GetCustomers")]
[ApiController]
public class CustomerAPIController : ControllerBase
{
[HttpGet]
public List<Customer> GetCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { Id = 1, Name = "John Hammond", Country = "United States" });
customers.Add(new Customer { Id = 2, Name = "Mudassar Khan", Country = "India" });
customers.Add(new Customer { Id = 3, Name = "Suzanne Mathews", Country = "France" });
customers.Add(new Customer { Id = 4, Name = "Robert Schidner", Country = "Russia" });
return customers;
}
}
The Startup class looks like below.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
The add another ASP.Net Core template project to consume the WebAPI.
Add customer class to Models folder.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Then add a controller and write below codes to it call the WebAPI url.
To use JsonConvert add Newtonsoft.Json reference to the project.
Namespaces
using System.Net;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc;
using WebAPI_Core_MVC.Models;
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
string apiUrl = "http://localhost:61130/api/CustomerAPI";
WebClient client = new WebClient();
string json = client.DownloadString(apiUrl + "/GetCustomers");
List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(json);
return View(customers);
}
}
Then add View to display the record.
View
@model IEnumerable<WebAPI_Core_MVC.Models.Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Id)</td>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Country)</td>
</tr>
}
</tbody>
</table>
</body>
</html>
Screenshot