StatusCode:405,ReasonPhrase:'Method Not Allowed'
StatusCode: 405, ReasonPhrase: 'Method Not Allowed'
我正在构建一个 Web 应用程序来使用 REST FULL API ASP.Net 核心 Web 服务,我在更新记录时遇到问题。
我正在尝试通过 ASP.Net 核心应用程序
从 API ASP.Net 核心调用 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);
}
}
}
我希望更新一条记录
我稍微清理了你的代码。试试这个方法
我删除了不必要的 using 块并使用单个 "application/json" 编码序列化了您的员工 class。
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);
}
我正在构建一个 Web 应用程序来使用 REST FULL API ASP.Net 核心 Web 服务,我在更新记录时遇到问题。 我正在尝试通过 ASP.Net 核心应用程序
从 API ASP.Net 核心调用 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);
}
}
}
我希望更新一条记录
我稍微清理了你的代码。试试这个方法
我删除了不必要的 using 块并使用单个 "application/json" 编码序列化了您的员工 class。
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);
}