StatusCode:405,ReasonPhrase:“不允许使用方法”

时间:2019-06-25 23:19:29

标签: asp.net-mvc api asp.net-core core-api

我正在构建一个Web应用程序以使用REST FULL API ASP.Net Core Web Service,更新记录时出现问题。 我正在尝试通过ASP.Net Core应用程序从API ASP.Net core调用Put方法

我有一个NullReferenceException:对象引用未设置为对象的实例。

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);
        }
     }
}

我希望更新记录

1 个答案:

答案 0 :(得分:0)

我稍微整理了一下代码。尝试这种方式

我使用了块删除了不必要的内容,并使用单一的“ application / json”编码序列化了您的员工类。

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);
 }