You could render the view as string like below:
public class EmployeesController : Controller
{
private readonly YourDbContext _context;
private readonly Microsoft.AspNetCore.Mvc.ViewEngines.ICompositeViewEngine _viewEngine;
public EmployeesController(ICompositeViewEngine viewEngine, YourDbContext context)
{
_viewEngine = viewEngine;
_context = context;
}
public async Task<string> DetailsSend(int id)
{
var details = await _context.Details
.FirstOrDefaultAsync(d => d.Id == id);
string view = "";
ViewData.Model = details;
using (var writer = new StringWriter())
{
ViewEngineResult viewResult = _viewEngine.FindView(ControllerContext, "Details", false);
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
ViewData,
TempData,
writer,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
view = writer.GetStringBuilder().ToString();
}
return view;
}
}
Then you could use HttpClient to call this api and get the result like below:
public async Task<IActionResult> Index()
{
var url = "https://localhost:portNumber/ControllerName/detailssend/1";//it should be the url of your api
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);
using (var content = response.Content)
{
//get the json result from your api
var result = await content.ReadAsStringAsync();
}
}