Hi rani,
Check this example. Now please take its reference and correct your code.
You nreed to configure the OutputFormatters in startup file.
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebAPI_Return_XML_Core_MVC
{
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(options =>
{
options.OutputFormatters.RemoveType(typeof(SystemTextJsonOutputFormatter));
options.ReturnHttpNotAcceptable = true;
}).AddXmlSerializerFormatters();
}
// 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.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Namespaces
using System.Data;
using System.IO;
using System.Net.Http;
Model
public class CustomerModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
string apiUrl = "http://localhost:1132/api/CustomerAPI/GetCustomers";
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(apiUrl).Result;
string result = "";
DataTable dt = new DataTable();
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
StringReader sr = new StringReader(result);
DataSet ds = new DataSet();
ds.ReadXml(sr);
dt = ds.Tables["CustomerModel"];
}
return View(dt);
}
}
APIController
[ApiController]
public class CustomerAPIController : ControllerBase
{
[Route("api/CustomerAPI/GetCustomers")]
[HttpGet]
public IActionResult GetCustomers()
{
List<CustomerModel> customers = new List<CustomerModel>();
customers.Add(new CustomerModel { Id = 1, Name = "John Hammond", Country = "United States" });
customers.Add(new CustomerModel { Id = 2, Name = "Mudassar Khan", Country = "India" });
customers.Add(new CustomerModel { Id = 3, Name = "Suzanne Mathews", Country = "France" });
customers.Add(new CustomerModel { Id = 4, Name = "Robert Schidner", Country = "Russia" });
return Ok(customers);
}
}
View
@model System.Data.DataTable
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
</tr>
@foreach (System.Data.DataRow dr in Model.Rows)
{
<tr>
<td>@dr["Id"]</td>
<td>@dr["Name"]</td>
<td>@dr["Country"]</td>
</tr>
}
</table>
</body>
</html>
Screenshot