is this how you are suppose to use update using api in asp.net?

public IActionResult Update(int id)
{
MatchDetails matchDetails = new MatchDetails();
HttpResponseMessage responseMessage = _httpClient.GetAsync($"MatchDetails/GetMatchDetails/{id}").Result;

if (responseMessage.IsSuccessStatusCode)
{
string data = responseMessage.Content.ReadAsStringAsync().Result;
matchDetails = JsonConvert.DeserializeObject<MatchDetails>(data);
}
return View(matchDetails);
}

[HttpPut]
public async Task<IActionResult> Update(int id, MatchDetails matchDetails)
{
try
{
string json = JsonConvert.SerializeObject(matchDetails);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");

HttpResponseMessage httpResponse = await _httpClient.PutAsync($"MatchDetails/UpdateMatchDetails/{id}", content);

if (!httpResponse.IsSuccessStatusCode)
{
_logger.LogError($"Failed to update match details: {httpResponse.StatusCode}");
return BadRequest();
}
}
catch (Exception ex)
{
_logger.LogError($"An error occurred while updating match details: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError);
}

return RedirectToAction(nameof(Index));
}
public IActionResult Update(int id)
{
MatchDetails matchDetails = new MatchDetails();
HttpResponseMessage responseMessage = _httpClient.GetAsync($"MatchDetails/GetMatchDetails/{id}").Result;

if (responseMessage.IsSuccessStatusCode)
{
string data = responseMessage.Content.ReadAsStringAsync().Result;
matchDetails = JsonConvert.DeserializeObject<MatchDetails>(data);
}
return View(matchDetails);
}

[HttpPut]
public async Task<IActionResult> Update(int id, MatchDetails matchDetails)
{
try
{
string json = JsonConvert.SerializeObject(matchDetails);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");

HttpResponseMessage httpResponse = await _httpClient.PutAsync($"MatchDetails/UpdateMatchDetails/{id}", content);

if (!httpResponse.IsSuccessStatusCode)
{
_logger.LogError($"Failed to update match details: {httpResponse.StatusCode}");
return BadRequest();
}
}
catch (Exception ex)
{
_logger.LogError($"An error occurred while updating match details: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError);
}

return RedirectToAction(nameof(Index));
}
This code shows error response 405 but my api is working fine if you dont understand what I am saying please tell me i will try my best
No description
No description
No description
No description
13 Replies
Angius
Angius8mo ago
Why are you using HttpClient to call one method from another...? Also, don't use .Result, never ever ever Use proper asynchronous code and await it Also also, ditch Newtonsoft.Json and use System.Text.Json
AbishkarKafle
AbishkarKafleOP8mo ago
ok uhh what do you mean? like use httpClientFactory
Angius
Angius8mo ago
No, I mean why are you calling the API in the controller? Presumably, both are in the same project?
AbishkarKafle
AbishkarKafleOP8mo ago
both are different project but they are running locally
Angius
Angius8mo ago
Ah, well, that changes things I guess And also makes me question why are they two different projects
AbishkarKafle
AbishkarKafleOP8mo ago
where should I call it what is the strandard?
Angius
Angius8mo ago
You should... not be calling the API from the controller
AbishkarKafle
AbishkarKafleOP8mo ago
i am learning to make api and integrate
Angius
Angius8mo ago
This project should also be connected to the database And should be calling that database
AbishkarKafle
AbishkarKafleOP8mo ago
to teh same database?
Angius
Angius8mo ago
Ah, so the MVC project is supposed to be the API consumer, then? Yes, you can connect more than one project to a database
AbishkarKafle
AbishkarKafleOP8mo ago
yes
Angius
Angius8mo ago
Carry on, then Far as the error goes... 405 means "method not allowed" So you're trying to POST to a PUT endpoint, GET from a DELETE endpoint, etc
Want results from more Discord servers?
Add your server