locked
Consume Rest API in Asp.net webforms project RRS feed

  • Question

  • User-699381365 posted

    Hello,

    I have a Asp.net webforms project and I have to consume an external REST API to send our data.

    I have two layers in my project, Presentation layer and service layer (like DAL - it has all my WCF services).

    Currently, there is a existing screen with save method which saves data to the application database. If the transaction is successful, then I have to post the same data to External API.

    So the question are

    1. In which layer(Presentation or Service) I need to implement the WEB API call?
    2. Should I create any Web API controller to consume the Rest API.


    Thanks.

    Tuesday, March 24, 2020 12:17 AM

All replies

  • User1120430333 posted

    You should be using HTTPClient() to consume a Restful API service using the HTTP verbs like Get, Post, Put or Delete.

    http://zetcode.com/csharp/httpclient/

    Where you implement it is up to you.

    Tuesday, March 24, 2020 5:20 AM
  • User-699381365 posted

    Thanks for your reply.

    I know using web api is the best practice for consuming external http requests.

    But in my case, I have asmx web service method which saves data to database and if the transaction is succeeded then I have to post the data via rest api.

    So from my asmx, can I make a call to my api controller to invoke rest api OR 

    can i call rest api directly from my asmx ? 

    I would like to know the best practice and at the same time I want to use web api controller class to consume Rest api service.

    Thanks,

    Raj

    Tuesday, March 24, 2020 8:49 PM
  • User475983607 posted

    So from my asmx, can I make a call to my api controller to invoke rest api OR 

    can i call rest api directly from my asmx ? 

    It is up to you.  You can call a REST Service from your web application or service.   Only you know which is best for your application. 

    Tuesday, March 24, 2020 8:57 PM
  • User1120430333 posted

    So from my asmx, can I make a call to my api controller to invoke rest api OR

    I don't see where a WebAPI controller comes into play here in your solution. Your solution is a client to a vendor's restful service. In order to consume the vendor's restful service, you must use HTTPClient code in your solution that is the client to the vendor's restful service to consume the services of the vendor's restful service. There is no WebAPI controller involved here.

    Where you implement the HTTPClient code in your solution is up to you. 

    Your HTTPClient code can be in the presentation or Web service layer in your solution. It's up to you.

    Wednesday, March 25, 2020 12:14 AM