HTTP Post - how to get the Id from the HTTP Post response and NOT from context.SaveChanges

wavemaster 311 Reputation points
2021-03-02T02:49:27.01+00:00

private void AddService()
{
Service service = new Service();
service.ServiceName = newService.ServiceName;
service.RoleId = 8;
service.IsProcedure = false;
service.ShortForm = newService.ServiceName;
service.Maincat = newService.MainCat;
service.Subcat = null;
service.Strength = null;
service.Package = null;
service.Size = null;
service.Note = null;
string baseUrl = "https://localhost:44384";
var httpClient = _clientFactory.CreateClient("ServerAPI");
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
var response = httpClient.PostAsJsonAsync($"{baseUrl}/api/Service/AddNewService", service);

    //need to do stuff here where I need the newly created ServiceId

}

How do I get the Id of the newly inserted record from <response>?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,195 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,398 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Wang-MSFT 1,051 Reputation points
    2021-03-02T07:16:33.53+00:00

    Hi, @MyLadys-4900,
    You should confirm one things that it will return the newly created Service entity from the HTTP Post response.
    You could get jsonString from response.

        var result = response.Result;  
    

    string jsonString = result.Content.ReadAsStringAsync().Result;
    Then you can deserialize your object from json string:

    Service returnService = JsonSerializer.Deserialize<Service>(jsonString);

    Now, you can get Id from returnService.Id.

    ---
    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
    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,
    Michael Wang

    0 comments No comments

5 additional answers

Sort by: Newest
  1. SM333 1 Reputation point
    2021-03-23T14:42:22.647+00:00

    Check your code....

    private void AddService()
    {Service service = new Service();

    Are you in need of a dot at the key\value pair,
    {
    Service.service = NewService()
    }

    ....and check your formatting...?
    Consistency will make your code easier to debug, as well as easier for others to read @MyLadys-4900
    Alt/Option + F10 will tab to the formatting toolbar.

    0 comments No comments

  2. wavemaster 311 Reputation points
    2021-03-05T15:25:54.6+00:00

    There was indeed something wrong with what I did. Got that fixed.

    This is what I ended up with:

        var response = await httpClient.PostAsync($"{baseUrl}/api/Service/AddNewService", content);
        var data = await response.Content.ReadAsStringAsync();
        Service result = new Service();
    
        if (response.IsSuccessStatusCode)
        {
            result = JsonSerializer.Deserialize<Service>(data);    
        }
        else 
        {
            errorMessage = data;
        }
    
        if (result != null) { // do stuff }
    

    This all works, thanks miwan2!

    1 person found this answer helpful.
    0 comments No comments

  3. wavemaster 311 Reputation points
    2021-03-03T14:33:15.167+00:00

    This targets NET5 so I have Swagger to look at to see what it thinks is the route:
    /api/Service/AddNewService

    0 comments No comments

  4. Michael Wang-MSFT 1,051 Reputation points
    2021-03-03T07:19:43.817+00:00

    System.InvalidOperationException: No route matches the supplied values

    It said the url {baseUrl}/api/Service/AddNewService", service); you post doesn't exist. Please check the route firstly.

    0 comments No comments