我怎样才能从 Asp.Net Web Api 2 得到 JSON 看起来像 Google API 响应的结果?
How can I get JSON result from Asp.Net Web Api 2 that looks like Google API response?
使用 ASP.net Web api 2 我如何以 google 的方式回复:
https://developers.google.com/maps/documentation/geocoding/intro
我的模特:
[DataContract]
public class ProductResponse
{
[DataMember]
public string Status_Message { get; set; }
[DataMember]
public List<Product> Products { get; set; }
}
我的控制器:
public ProductResponse GetAllProducts()
{
ProductResponse response = new ProductResponse();
try
{
using (SQLServerWrapper sqlConn = new SQLServerWrapper())
{
using (SqlCommand command = sqlConn.PrepareCommand(Product.SQLSelect))
{
SqlDataReader reader = command.ExecuteReader();
response.Products = reader.Select<Product>(Product.FromDataReader).ToList();
}
}
response.Status = "OK";
}
catch (Exception ex)
{
response.Status = "ERROR";
}
return response;
}
一切正常。我的问题是如何将响应切换为浏览器可读的 JSON 类型?
简单,只需在 json 中请求答案 :)
序列化的类型由您请求中的 header 或不存在决定。
Content-Type: application/json
Accept: application/json
默认情况下 asp.net web api 可以在 xml 或 json 中序列化。
使用 ASP.net Web api 2 我如何以 google 的方式回复: https://developers.google.com/maps/documentation/geocoding/intro
我的模特:
[DataContract]
public class ProductResponse
{
[DataMember]
public string Status_Message { get; set; }
[DataMember]
public List<Product> Products { get; set; }
}
我的控制器:
public ProductResponse GetAllProducts()
{
ProductResponse response = new ProductResponse();
try
{
using (SQLServerWrapper sqlConn = new SQLServerWrapper())
{
using (SqlCommand command = sqlConn.PrepareCommand(Product.SQLSelect))
{
SqlDataReader reader = command.ExecuteReader();
response.Products = reader.Select<Product>(Product.FromDataReader).ToList();
}
}
response.Status = "OK";
}
catch (Exception ex)
{
response.Status = "ERROR";
}
return response;
}
一切正常。我的问题是如何将响应切换为浏览器可读的 JSON 类型?
简单,只需在 json 中请求答案 :)
序列化的类型由您请求中的 header 或不存在决定。
Content-Type: application/json
Accept: application/json
默认情况下 asp.net web api 可以在 xml 或 json 中序列化。