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

Daniel 156 Reputation points
2021-07-06T21:02:08.25+00:00

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://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio#call-the-web-api-with-javascript

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
Developer technologies | C#
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.
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-07-07T03:17:14.753+00:00

    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

    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

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.