Search code examples
asp.net-mvcapiasp.net-corecore-api

StatusCode: 405, ReasonPhrase: 'Method Not Allowed'


i'm building a Web Application to consume A REST FULL API ASP.Net Core Web Service, i have Problem when i update a records. i'm trying to Call Put Method from API ASP.Net core by ASP.Net Core application

I have got an NullReferenceException: Object reference not set to an instance of an object.

public async Task<IActionResult> UpdateEmployee(int id)
{
    Employee employee = new Employee();
    using(var httpClient=new HttpClient())
    {
        using(var response = await httpClient.GetAsync("myURI/employee/" + id))
        {
             string apiResponse = await 
             response.Content.ReadAsStringAsync();

             employee = JsonConvert.DeserializeObject<Employee>apiResponse);
        }
    }
    return View(employee);                   
}


[HttpPost]
public async Task<IActionResult> UpdateEmployee(Employee employee)
{
    Employee receivedemployee = new Employee();
    using(var httpClient=new HttpClient())
    {
        var content = new MultipartFormDataContent();

       content.Add(new             
        StringContent(employee.EmployeeId.ToString(),Encoding.UTF8, 
            "application/json"), "id");
            content.Add(new 
        StringContent(employee.FirstName,Encoding.UTF8, 
          "application/json"),"FirstName");
            content.Add(new StringContent(employee.LastName, 
         Encoding.UTF8, "application/json"), "LastName");
            content.Add(new StringContent(employee.DateOfBirth.ToString(), 
            Encoding.UTF8, "application/json"), "Email");
            content.Add(new StringContent(employee.PhoneNumber, 
            Encoding.UTF8, "application/json"), "DateOfBirth");
            content.Add(new StringContent(employee.Email, Encoding.UTF8, 
           "application/json"), "Email");

            using (var response = await httpClient.PutAsync("myURI/api/employee", content))
            {

                string apiResponse = await response.Content.ReadAsStringAsync();
                ViewBag.Result = "Success";
                receivedemployee = JsonConvert.DeserializeObject<Employee>(apiResponse);
            }
            return View(receivedemployee);
        }
     }
}

i expected updating a record


Solution

  • I cleaned up your code a bit. try it this way

    I removed unnecessary using blocks and serialized your employee class with a single "application/json" encoding.

    public async Task<IActionResult> UpdateEmployee(int id)
    {
        Employee employee = new Employee();
        var httpClient = new HttpClient();
    
        var request = new HttpRequestMessage
          (HttpMethod.Get, $"myURI/employee/{id}");
    
        var response = await httpClient.SendAsync(request);
        string apiResponse = await response.Content.ReadAsStringAsync();
    
        employee = JsonConvert.DeserializeObject<Employee>(apiResponse);
    
        return View(employee);
    }
    
    [HttpPost]
    public async Task<IActionResult> UpdateEmployee(Employee employee)
    {
        Employee receivedEmployee = new Employee();
    
        var httpClient = new HttpClient();
        var request = new HttpRequestMessage(HttpMethod.Put, $"myURI/employee/{employee.EmployeeId}")
        {
             Content = new StringContent(new JavaScriptSerializer().Serialize(employee), Encoding.UTF8, "application/json")
        };
    
         var response = await httpClient.SendAsync(request);
    
         string apiResponse = await response.Content.ReadAsStringAsync();
         ViewBag.Result = "Success";
         receivedEmployee = JsonConvert.DeserializeObject<Employee>(apiResponse);
    
         return View(receivedEmployee);
     }