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

Daniel 156 Reputation points


I’ve looked into using a 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?

A set of technologies in the .NET Framework for building web applications and XML web services.
4,124 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,171 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JasonPan - MSFT 4,201 Reputation points Microsoft Vendor

    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 core mvc app, like below:

    First, you need add services.AddHttpClient(); in Startup.cs .

    public void ConfigureServices(IServiceCollection services)  

    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,  
                var client = _clientFactory.CreateClient();  
                var response = await client.SendAsync(request);  
                if (response.IsSuccessStatusCode)  
                    return await response.Content.ReadAsStringAsync();  
                    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,

    1 person found this answer helpful.