2

I need to save the changes I make in my model through API call in my database. I have checked my API is working fine when I am running it individually on Web. But its giving me an error StatusCode: 405, ReasonPhrase: 'Method Not Allowed'. I am trying to send and object and trying to see whether the request made was completed or not. When I am trying to debug it, it is not sending hit on my API controller.

Here is my model class:

public class Customer
{
    [Required]
    public Guid CustomerId { get; set; }
    public int Age { get; set; }
    public int Phone { get; set; }
}

PUT Method in API:

[HttpPut]
[Route("api/[controller]/{customer}")]
public IActionResult EditCustomer(Customer customer)
{
    var cust = _customerData.EditCustomer(customer);
    if (cust == string.Empty)
    {
        return Ok();
    }
    else
    {
        return new StatusCodeResult(StatusCodes.Status500InternalServerError);
    }
}

The method I am using in project to call API:

using (HttpClient client = new HttpClient())
{
    client.BaseAddress = new Uri(apiBaseUrl);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(
        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
    );
    var sum = await client.PutAsJsonAsync("api/Customer/", customer);
    if (sum.StatusCode == System.Net.HttpStatusCode.OK)
    {
        return RedirectToActionPermanent(actionName: "SingIn");
    }
    else
    {
        TempData["msg"] = "There is an error";
        return View();
    }

where baseaddress= {https://localhost:44398/}


EditCustomer Method

public string EditCustomer(Customer customer)
{
    try
    {
        var pro = _customerContext.Customer.Where(e => e.CustomerId == customer.CustomerId).FirstOrDefault();
        pro.Age = customer.Age;
        pro.Phone = customer.Phone;
        pro.Name = customer.Name;
        _customerContext.Entry(pro).State = EntityState.Modified;
        _customerContext.SaveChanges();
    }
    catch(Exception e)
    {
        return e.Message;
    }
    return string.Empty;
}
2
  • Hi @Psychonaut007, I suggest you can share your EditCustomer corresponding controller . Also please check your customer if it has correct value. Commented Oct 22, 2021 at 5:42
  • Hello @Rena I have shared the mentioned method but the problem is I am not receiving a hit in controller at the first place. Commented Oct 25, 2021 at 5:36

2 Answers 2

4

You need to fix your action route by removing {Customer}, since you send customer in request body, not as a route value

 [Route("~/api/Customer")]

and request

  var sum = await client.PutAsJsonAsync("/api/Customer", customer);

or better fix the acttion route name to meaningfull

  [Route("~/api/EditCustomer")]

and

var sum = await client.PutAsJsonAsync("/api/EditCustomer", customer);

AsJsonAsync sometimes causes problems

try this code

var json = JsonSerializer.Serialize(customer);
//or if you are using Newtonsoft
var json = JsonConvert.SerializeObject(customer);

var contentData = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PutAsync("/api/Customer", contentData);
              
if (response.IsSuccessStatusCode)
    return RedirectToActionPermanent("SingIn");
else
{
 TempData["msg"] = "There is an error";
 return View();
}

but IMHO I would prefer to use

client.PostAsync("/api/EditCustomer", contentData); 

instead of Put.

and added [FromBody] to action

 [HttpPost("~/api/EditCustomer")]
 public IActionResult EditCustomer([FromBody] Customer customer)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply @Serge! I have applied above changes and got this error StatusCode: 400, ReasonPhrase: 'Bad Request
I think it is your action problem. Use debugger inside of the API action and check if customer is not empty
0

I am no pro in web APIs but I suspect it could be due to the fact that the API expects customer to be in request URL.

Try and change the API route to [Route("api/[controller]")]

This could've been a comment but I don't have enough reputation :)

1 Comment

Thanks this is working but i am looking for a solution where i can pass whole object just to make it more feasible in dealing with large number of variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.