Hirani,
Refer below code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
First Create a WEB API project with ASP.Net Core.
Then open Startup.cs file and replace below codes.
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
Then add a class in Model folder and erite below code.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Then add CustomerController in Controllers folder and write below code.
[Route("api/")]
[ApiController]
public class CustomerController : ControllerBase
{
[HttpGet]
[Route("GetCustromers")]
public List<Customer> GetCustromers()
{
List<Customer> customers = new List<Customer>();
string constr = @"Data Source=.;Initial Catalog=Test;UID=sa;PWD=pass@123;";
string query = "SELECT * FROM Customers";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new Customer
{
Id = Convert.ToInt32(sdr["CustomerId"]),
Name = Convert.ToString(sdr["Name"]),
Country = Convert.ToString(sdr["Country"])
});
}
}
con.Close();
}
}
return customers;
}
}
Now add another ASP.Net Core project to consume the Web API in it.
Model
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public List<Customer> Customers { get; set; }
}
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
Customer customer = new Customer();
customer.Customers = SearchCustomers();
return View(customer);
}
private static List<Customer> SearchCustomers()
{
string apiUrl = "https://localhost:44362/api/GetCustromers";
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = Encoding.UTF8;
string json = client.DownloadString(apiUrl);
List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(json);
return customers;
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model WebAPI_ASP.Net_Core_Page.Models.Customer
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Country)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Customers)
{
<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