如何通过REST URL传递JSON对象
本文关键字:JSON 对象 传递 URL 何通过 REST | 更新日期: 2023-09-27 18:23:39
我在WEBUI控制器中有一个方法GetCustomerDetails(如下)
public bool GetCustomerDetails(string customer)
{
Uri CustUri = null;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://online/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("api/customers/", customer).Result;
if (response.IsSuccessStatusCode)
{
CustUri = response.Headers.Location;
}
return false;
}
the above method has to hit the below method(API controller).
public string PostCustomer(string customerJsonString)
{
CustomerDetailDto customer = JsonConvert.DeserializeObject<CustomerDetailDto>(customerJsonString);
bool res = _customerService.SaveOrUpdateCustomer(customer);
if (res)
{
....
}
return something;
}
但作为回应(上面的WEBUI),我得到错误消息作为
{StatusCode:405,ReasonPhrase:"不允许使用方法",版本:1.1,内容:System.Net.Http.StreamContent,标头:{Pragma:无缓存连接:关闭缓存控制:无缓存日期:2013年7月17日,星期三12:17:49 GMT服务器:ASP.NET服务器:开发服务器:Server/11.0.0 X-AspNet-版本:4.0.30319内容长度:73
内容类型:application/json;charset=utf-8过期:-1}}
有谁能帮我解决这个问题吗。
很抱歉来不及回答您的问题,希望您的解决方案已经实现。我发布这个解决方案是为了帮助遇到同样情况的其他人。
API控制器方法PostCustomer必须包含一个包含有效方法列表的Allow头。因此,我们有以下实现。
[System.Web.Http.HttpPost]
public string PostCustomer(string customerJsonString)
{
CustomerDetailDto customer = JsonConvert.DeserializeObject<CustomerDetailDto>(customerJsonString);
bool res = _customerService.SaveOrUpdateCustomer(customer);
if (res)
{
....
}
return something;
}
添加HttpPost头将有助于WebUI控制器与上述方法进行对话。
谢谢,Neeraj