How I integrate web API into my ASPx (VB) Project, Post/Get response values

Aypn CNN 386 Reputation points
2021-11-29T14:15:40.873+00:00

Hi

I'm a very beginner for web API concepts, I am developing web apps using VS2019 with VB, I have enclosed (sample api) my partner web api, I request you to how I integrate this into my aspx web, how I pass values from my Aspx page to API and how I get response value from that API, pls help me on this. 153325-abc-api-testpostman-collectionjson.txt

153384-myaspxpage.txtResponse Values 101 Like Sucess,102 Invalid Inputs, 106 Err.

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
153 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 38,271 Reputation points
    2021-11-29T16:43:49.523+00:00

    That is quite a big ask for the forums. Have you looked at any of the existing documentation and samples on how to call a REST API from .NET? There are plenty of examples. APIs vary wildly so some of this requires that you understand how the API you're calling works.

    https://www.programmingnotes.org/6229/vb-net-how-to-send-post-process-a-rest-api-web-request-using-vb-net/
    https://social.msdn.microsoft.com/Forums/en-US/bd62538b-1adc-4064-86ec-c093cd754e08/vbnet-rest-api-call-with-basic-authorization?forum=vbgeneral
    https://stackoverflow.com/questions/56173249/calling-rest-api-from-visual-basic

    Writing API calls by hand is pretty easy but requires experience. A faster approach to get started, if you're using Visual Studio 2019 or newer, is to use Connected Services. This assumes that the API you're calling supports OpenAPI (all public ones do). If this is true then right click your project and select Add -> Connected Service. In the UI select the option to add a service reference to OpenAPI. Set the URL to the APIs OpenAPI spec (get this URL from the provider) and then click Finish to have it auto-generate all the code for you. You can then calling the API directly using standard .NET types.

    If you want to do it manually then it is recommended you create a wrapper class around the client. HttpClient is the core type needed for this to work. Here's an example of what it might look like given just 1 request in your sample.

       Imports System  
       Imports System.Net.Http  
       Imports System.Net.Http.Json  
       Imports System.Threading  
         
       Class AbcClient  
           Sub New(client As HttpClient)  
               _client = client  
           End Sub  
         
           Async Function XyxPushAsync(name As String, title As String, phone As String, file As Byte()) As Task  
               Dim body = New With {.title = title, .name_applicant = name, .mobile = phone, .upload_file = file}  
         
               Using response As HttpResponseMessage = Await _client.PostAsJsonAsync("xyx_push_api.php", body)  
                   response.EnsureSuccessStatusCode()  
               End Using  
           End Function  
         
           Private ReadOnly _client As HttpClient  
       End Class  
         
       Module Program  
           Sub Main(args As String())  
               Dim client = New AbcClient(_httpClient)  
         
               client.XyxPushAsync("Bob", "Director", "5551234567", Array.Empty(Of Byte)).Wait()  
           End Sub  
         
           Private ReadOnly _httpClient As HttpClient = New HttpClient() With {  
               .BaseAddress = New Uri("http://test.xyz.com/abc_webservice/")  
           }  
       End Module  
    

    Some notes on this code.

    1. HttpClient should only be created once per application per base URL.
    2. URL passed to HttpClient must end with slash.
    3. The sample wasn't clear what a "file" was so I assume a byte array. This probably isn't correct so you'll need to adjust.
    4. If an API returns a response then you'll have to convert the response back to an object you care about using a JSON deserializer.
    5. APIs will fail periodically so all this should be done using exception handling and the app needs to handle failures.
    6. The AbcClient class can be expanded over time to include additional functions for each API endpoint you intend to call.
    7. The actual client call contains just the part of the URL after the base address that the client already defined.

  2. Yijing Sun-MSFT 7,026 Reputation points
    2021-11-30T05:21:01.087+00:00

    Hi @Aypn CNN ,
    If you want to integrate web api into the webform application,you need do 5 Simple Steps:

    1. Create a New Web Forms Application
    2. Add Model
    3. Add Controller class
    4. Add Routing Info to Global.asax
    5. Make a Client Call using jquery.

    The code is like this:
    153569-new-text-document-5.txt
    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.