question

dncoso avatar image
0 Votes"
dncoso asked JasonPan-MSFT edited

Am I not supposed to use json api in net.core?

Hi,

I’ve looked into using a Asp.net core API project with my existing MVC.

However there doesn’t seem to be some type of standard way of consuming JSON in MVC or am I missing something?

According to this article I only read it as creating the web api and that it should only be called by JavaScript client side and not from the mvc app?

https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio#call-the-web-api-with-javascript

dotnet-csharpdotnet-aspnet-core-mvc
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

However there doesn’t seem to be some type of standard way of consuming JSON in MVC or am I missing something?

The HttpClient is commonly used to make HTTP requests from C#. In ASP.NET Core, the HttpClient is typically configured for dependency injection.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-5.0


0 Votes 0 ·

Yes!

This was what I was looking for. Using the

`JsonSerializer.DeserializeAsync
<IEnumerable<GitHubBranch>>(responseStream);

I got it to work.

0 Votes 0 ·

Why can’t I mark this as the answer?

0 Votes 0 ·

1 Answer

JasonPan-MSFT avatar image
1 Vote"
JasonPan-MSFT answered JasonPan-MSFT edited

Hi @dncoso

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://docs.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

112354-capture.jpg


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




capture.jpg (98.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the response!

0 Votes 0 ·