Hi @Daniel
Webapi can respond to any client that can send Http requests. So webapi is not limited to javascript client access.
You can use HttpClient in asp.net core mvc app, like below:
First, you need add services.AddHttpClient();
in Startup.cs .
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddHttpClient();
}
Secod, you can invoke http request in your controller.
public class HomeController: Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IHttpClientFactory _clientFactory;
public HomeController(ILogger<HomeController> logger, IHttpClientFactory clientFactory)
{
_logger = logger;
_clientFactory = clientFactory;
}
public async Task<String> Test()
{
var request = new HttpRequestMessage(HttpMethod.Get,
"https://learn.microsoft.com/en-us/");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return "Request Failed ";
}
}
}
Result Picture
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Jason