markjprice / cs11dotnet7

Repository for the Packt Publishing book titled "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals" by Mark J. Price
566 stars 206 forks source link

Exercise 15.2 #74

Closed James-Sutanto closed 1 year ago

James-Sutanto commented 1 year ago

Hi Mark, this is James, and I am currently having trouble with exercise 15.2, I was able to submit a GET request through the Northwind web service, but I can't submit a post or delete request. What methods should I use from the httpclient class(I tried .PostAsJsonAsync, and DeleteAsync)? Am I using the right url for the method url, which is https://localhost:5002/api/customers and https://localhost:5002/api/customers/{id}? Where can I find the solution to this exercise in your github repo?

Thank you for all the help, James Sutanto, Aspiring Unity Developer

markjprice commented 1 year ago

Hi James,

Yes, PostAsJsonAsync and DeleteAsync should be what you need to use.

You will need two <form> elements for the visitor to enter a new customer and to enter or click a customer ID to delete, with matching action methods similar to the following code:

public async Task<IActionResult> AddCustomer(Customer customer)
{
  HttpClient client = clientFactory.CreateClient(
    name: "Northwind.WebApi");

  HttpResponseMessage response = await client.PostAsJsonAsync(
    requestUri: "api/customers", value: customer);

  if (response.IsSuccessStatusCode)
  {
    // Store a success message in TempData to show on the page.
  }

  // Optionally, get the created customer back as JSON
  // so the user can see the assigned ID, for example.
  Customer? model = await response.Content
    .ReadFromJsonAsync<Customer>();

  return Redirect("customers");
}

public async Task<IActionResult> DeleteCustomer(string customerId)
{
  HttpClient client = clientFactory.CreateClient(
    name: "Northwind.WebApi");

  HttpResponseMessage response = await client.DeleteAsync(
    requestUri: $"api/customers/{customerId}");

  if (response.IsSuccessStatusCode)
  {
    // Store a success message in TempData to show on the page.
  }

  return Redirect("customers");
}

I have not created a solution for Exercise 15.2 so I will do it ASAP, upload it to the GitHub repository, and ping you on here when it's ready.

Thank you for your patience.

James-Sutanto commented 1 year ago

Thank you so much

James-Sutanto commented 1 year ago

Hi Mark, I added your code to my project and it seems like there might be a problem with my form. Do you know what is wrong with my form? I ran the code in my debugger and it seems that the form is saving the correct values to the corresponding values in the customer class.

`@using Packt.Shared @model Customer

Create Customer

`
James-Sutanto commented 1 year ago

Hi Mark, I added your code to my project and it seems like there might be a problem with my form. Do you know what is wrong with my form? I ran the code in my debugger and it seems that the form is saving the correct values to the corresponding values in the customer class.

`@using Packt.Shared @model Customer

Create Customer

Customer ID:

<div>
    <label asp-for="CompanyName">Company Name:</label>
    <input asp-for="CompanyName" />
</div>

<div>
    <label asp-for="ContactName">Contact Name:</label>
    <input asp-for="ContactName" />
</div>

<div>
    <label asp-for="ContactTitle">Contact Title:</label>
    <input asp-for="ContactTitle" />
</div>

<div>
    <label asp-for="Address">Address:</label>
    <input asp-for="Address" />
</div>

<div>
    <label asp-for="City">City:</label>
    <input asp-for="City" />
</div>

<div>
    <label asp-for="Region">Region:</label>
    <input asp-for="Region" />
</div>

<div>
    <label asp-for="PostalCode">Postal Code:</label>
    <input asp-for="PostalCode" />
</div>

<div>
    <label asp-for="Country">Country:</label>
    <input asp-for="Country" />
</div>

<div>
    <label asp-for="Phone">Phone:</label>
    <input asp-for="Phone" />
</div>

<div>
    <label asp-for="Fax">Fax:</label>
    <input asp-for="Fax" />
</div>
<div>
    <label asp-for="Orders">Orders:</label>
    <input asp-for="Orders"/>
</div>

<!-- Add other form fields as needed -->

<button type="submit">Create</button>

`

The error I am getting is a 500 internal server error.

markjprice commented 1 year ago

I cannot see your <form> element to see what the problem might be.

But I've added a suggested solution for the exercise and an improvement item here: https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/improvements.md#page-700---exercise-152--practice-creating-and-deleting-customers-with-httpclient

James-Sutanto commented 1 year ago

Thank you for your help, I'm sure I will be able to fix my code with your solution.