locked
Consume Rest api from MVC? RRS feed

  • Question

  • User-1104215994 posted

    Hello guys,

    I would like to implement an app which is a middleware. There is a rest API host lets call it A, there is a client which is B and my app C in the middle of them. Client B calls my methods and my app calls rest API host A. I return back <g class="gr_ gr_563 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="563" data-gr-id="563">response</g> from A to the client B. Is there any tutorial/advice that I can follow?

    Best Regards.

    Tuesday, January 22, 2019 7:51 AM

Answers

All replies

  • User753101303 posted

    Hi,

    See https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client for consuming a web API from C#. It seems either you want to create a ready to use client pr maybe a service calling other services behind the scene.

    Edit: or in the context of ASP.NET Core see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.2 (but I'm not sure it's really related to what you want)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 22, 2019 8:35 AM
  • User1120430333 posted

    Hello guys,

    I would like to implement an app which is a middleware. There is a rest API host lets call it A, there is a client which is B and my app C in the middle of them. Client B calls my methods and my app calls rest API host A. I return back response from A to the client B. Is there any tutorial/advice that I can follow?

    Best Regards.

    What would be the purpose on this  middle-man program of yours?  The only thing that would make any sense is your program is a possible service layer classlib project that would consume the WebAPI that acts upon the WebAPI controllers and the objects they use on the behalf of the client program, a proxy

    https://datatellblog.wordpress.com/2015/03/17/service-layer-mvc-controllers-and-dependency-injection/

    Tuesday, January 22, 2019 8:51 AM
  • User1520731567 posted

    Hi cenk1536,

    We could use HttpClient.

    For example:

     using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:64189/api/");
                //HTTP GET
                var responseTask = client.GetAsync("student");
                responseTask.Wait();
    
                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync<IList<StudentViewModel>>();
                    readTask.Wait();
    
                    students = readTask.Result;
                }
                else //web api sent error response 
                {
                    //log response status here..
    
                    students = Enumerable.Empty<StudentViewModel>();
    
                    ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
                }
            }

    To consume Web API in ASP.NET MVC server side we can use HttpClient in the MVC controller.

    HttpClient sends a request to the Web API and receives a response.

    We then need to convert response data that came from Web API to a model and then render it into a view.

    You could refer to these tutorial about how to Consume Rest api from MVC:

    http://www.tutorialsteacher.com/webapi/consume-web-api-get-method-in-aspnet-mvc

    https://www.c-sharpcorner.com/article/consuming-web-api-in-asp-net-mvc-part-two/

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 3:00 AM
  • User-1104215994 posted

    I found this tutorial. https://www.youtube.com/watch?v=vN9NRqv7xmY

    Thursday, January 24, 2019 7:30 AM