【发布时间】:2016-08-26 10:18:31
【问题描述】:
查看我的 Web api 控制器操作。我从我的操作返回一个响应类,其中包含客户数据、状态和消息等,但是当我从浏览器调用我的 Web 操作时,操作仅返回此符号 {},这很奇怪。查看我的网络 api 代码
我的代码如下
[RoutePrefix("api/customer")]
public class CustomerController : ApiController
{
static readonly ICustomerRepository repository = new CustomerRepository();
[HttpGet, Route("GetAll")]
public HttpResponseMessage GetAllCustomers()
{
var Response=new Response(true, "SUCCESS", repository.GetAll());
//return Response;
//return Request.CreateResponse(HttpStatusCode.OK, Response);
HttpResponseMessage response = Request.CreateResponse<Response>(HttpStatusCode.OK, Response);
return response;
}
[HttpGet, Route("GetByID/{customerID}")]
public Response GetCustomer(string customerID)
{
Customer customer = repository.Get(customerID);
if (customer == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return new Response(true, "SUCCESS", customer);
//return Request.CreateResponse(HttpStatusCode.OK, response);
}
[HttpGet, Route("GetByCountryName/{country}")]
public IEnumerable<Customer> GetCustomersByCountry(string country)
{
return repository.GetAll().Where(
c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
}
public HttpResponseMessage PostCustomer(Customer customer)
{
customer = repository.Add(customer);
var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID });
response.Headers.Location = new Uri(uri);
return response;
}
public void PutProduct(string customerID, Customer customer)
{
customer.CustomerID = customerID;
if (!repository.Update(customer))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
public void DeleteProduct(string customerID)
{
Customer customer = repository.Get(customerID);
if (customer == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
repository.Remove(customerID);
}
}
public class Response
{
bool IsSuccess = false;
string Message;
object ResponseData;
public Response(bool status, string message, object data)
{
IsSuccess = status;
Message = message;
ResponseData = data;
}
}
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
}
我是这样从winform using httpclient打电话的
var baseAddress = "http://localhost:38762/api/customer/GetAll";
using (var client = new HttpClient())
{
using (var response = client.GetAsync(baseAddress).Result)
{
if (response.IsSuccessStatusCode)
{
var customerJsonString = await response.Content.ReadAsStringAsync();
var cust = JsonConvert.DeserializeObject<Response>(customerJsonString);
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
}
告诉我 GetAll actions 的代码有什么问题,它没有返回 json 而是返回 {}
我必须返回我的响应类而不是IEnumerable<Customer>,所以请告诉我要在代码中更改什么的路径。
如果我的方法看起来像
[HttpGet, Route("GetAll")]
public Response GetAllCustomers()
{
var Response = new Response(true, "SUCCESS", repository.GetAll());
//return Response;
//return Request.CreateResponse(HttpStatusCode.OK, Response);
//HttpResponseMessage response = Request.CreateResponse<Response>(HttpStatusCode.OK, Response);
return Response;
}
OR
[HttpGet, Route("GetAll")]
public HttpResponseMessage GetAllCustomers()
{
var Response=new Response(true, "SUCCESS", repository.GetAll());
//return Response;
//return Request.CreateResponse(HttpStatusCode.OK, Response);
HttpResponseMessage response = Request.CreateResponse<Response>(HttpStatusCode.OK, Response);
return response;
}
但不返回任何数据或 json。只返回 {} 表示 null。
这样我就可以向我的 web api 发出指令,结果它应该返回 json。
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
【问题讨论】:
标签: asp.net-web-api httpclient