Hi All
I created two projects, an ASP.NET Core 7 Web API, and an ASP.NET Core 7 MVC project.
URL of my ASP.NET Core 7 Web API project is
localhost:7271
Now my question is how to configure this url in the ASP.NET Core 7 MVC project and call inside the controller. I am in learning stage. So please help me to resolve the issue.
In which place we have to write the API configuration in the ASP.NET Core 7 MVC project. Thanks!

ASP.NET Core 7 MVC project:


Sachin SinghRank #66 · 26.2K reputationPosted Oct 5, 2023, 1:31 AM EDT
@Sneha, the process is still the same you can call your api using ajax or HttpClient or RestClient
You can create an extension method for resulability (Deserialize to Objects) and then use it
public static class HttpClientExtensions { public static async Task ReadContentAsync(this HttpResponseMessage response) { if (response.IsSuccessStatusCode == false) throw new ApplicationException($"Something went wrong calling the API: {response.ReasonPhrase}"); var dataAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var result = JsonSerializer.Deserialize( dataAsString, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); return result; } }Then on Controller or any class library service project
public class YourClass { private readonly HttpClient _client; public const string BasePath = "/api/find"; public YourClass(HttpClient client) { _client = client ?? throw new ArgumentNullException(nameof(client)); } public async Task> Find() { var response = await _client.GetAsync(BasePath); return await response.ReadContentAsync>(); } }Tahir AnsariRank #366 · 6.8K reputationPosted Oct 4, 2023, 11:39 PM EDT
You need to create api code first in asp.net core api project.
Then consume in asp.net core MVC and configure based address of your api in appsettings.json like this
{
"ApiSettings": {
"ApiBaseUrl": "http://localhost:7271"
},
// Other settings...
}
and call it in controller while consuming api
Sneha KRank #2,496 · 563 reputationPosted Oct 5, 2023, 1:13 AM EDT
Hi Thahir,
i configured the api url in app settings.json file now how i call inside controller . In old technology we will use Http client here in dot net 7 mvc project how i call this any sample code pls?
Sneha KRank #2,496 · 563 reputationPosted Oct 5, 2023, 1:44 AM EDT
Hi Sachin
i will check it and let you know
Amit MohantyRank #112 · 20K reputationPosted Oct 5, 2023, 1:47 AM EDT
You can use RestClient to call API in your ASP.NET Core 7 MVC project.
GET Method:
string baseUrl = "your base url"; var restClient = new RestClient(baseUrl); var restRequest = new RestRequest("api/endpoint", Method.Get); restRequest.AddHeader("Content-Type", "application/json"); RestResponse restResponse = await restClient.ExecuteAsync(restRequest); if (restResponse.IsSuccessStatusCode) { // Use the response content here } else { // Handle the error }POST Method:
var restClient = new RestClient(baseUrl); var restRequest = new RestRequest("api/endpoint", Method.Post); var body = new { key1 = "value1", key2 = "value2" }; restRequest.AddBody(body, "application/json"); RestResponse restResponse = await restClient.ExecuteAsync(restRequest); if (restResponse.IsSuccessStatusCode) { // Use the response content here } else { // Handle the error }