I have .net core solution with controller return data in view I want to send url to this project as Api and return data in web form application
I am not good in .net core and Api technology
The controller code
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using GrandstreamClient.Manager;
using GrandstreamClient.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace GrandstreamClient.Controllers
{
public class CDRController : Controller
{
RestManager manager;
public CDRController()
{
manager = new RestManager();
}
public async Task<IActionResult> Index()
{
using (HttpClientHandler httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
var credential = new NetworkCredential("username", "password");
httpClientHandler.Credentials = credential;
httpClientHandler.PreAuthenticate = true;
using (HttpClient client = manager.Client(httpClientHandler))
{
//HttpResponseMessage response = await client.GetAsync("todos/1");
HttpResponseMessage response = await client.GetAsync("cdrapi?format=json");
RootObject list = new RootObject();
List<CDR> ResultList = new List<CDR>();
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
//list = JsonConvert.DeserializeObject<ObjTest>(result);
list = JsonConvert.DeserializeObject<RootObject>(result);
foreach (var item in list.cdr_root)
{
if (!string.IsNullOrEmpty(item.recordfiles))
{
item.recordfiles = $"{client.BaseAddress}recapi?filename={item.recordfiles}";
}
if (item.main_cdr != null)
{
item.main_cdr.recordfiles = string.IsNullOrEmpty(item.main_cdr.recordfiles) ? string.Empty : $"{client.BaseAddress}recapi?filename={item.main_cdr.recordfiles}";
}
if (item.sub_cdr_1 != null)
{
item.sub_cdr_1.recordfiles = string.IsNullOrEmpty(item.sub_cdr_1.recordfiles) ? string.Empty : $"{client.BaseAddress}recapi?filename={item.sub_cdr_1.recordfiles}";
}
if (item.sub_cdr_2 != null)
{
item.sub_cdr_2.recordfiles = string.IsNullOrEmpty(item.sub_cdr_2.recordfiles) ? string.Empty : $"{client.BaseAddress}recapi?filename={item.sub_cdr_2.recordfiles}";
}
if (item.sub_cdr_3 != null)
{
item.sub_cdr_3.recordfiles = string.IsNullOrEmpty(item.sub_cdr_3.recordfiles) ? string.Empty : $"{client.BaseAddress}recapi?filename={item.sub_cdr_3.recordfiles}";
}
if (item.sub_cdr_4 != null)
{
item.sub_cdr_4.recordfiles = string.IsNullOrEmpty(item.sub_cdr_4.recordfiles) ? string.Empty : $"{client.BaseAddress}recapi?filename={item.sub_cdr_4.recordfiles}";
}
ResultList.Add(item);
//return Redirect(item.recordfiles);
}
}
return View(ResultList);
}
}
}
}
}
startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace GrandstreamClient
{
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.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// 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.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=CDR}/{action=Index}/{id?}");
});
}
}
}
can any one help me to solve this