[英]How can I get JSON result from Asp.Net Web Api 2 that looks like Google API response?
Using ASP.net Web api 2 how can I response in google like manner: https://developers.google.com/maps/documentation/geocoding/intro 如何使用ASP.net Web API 2以类似Google的方式进行响应: https : //developers.google.com/maps/documentation/geocoding/intro
My model: 我的模特:
[DataContract]
public class ProductResponse
{
[DataMember]
public string Status_Message { get; set; }
[DataMember]
public List<Product> Products { get; set; }
}
My Controller: 我的控制器:
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;
}
All that works good. 一切正常。 My question how can I switch reponse to JSON type that is readable by browsers?
我的问题是如何将响应切换为浏览器可读的JSON类型?
Simple, just request an answer in json :) 简单,只需在json中请求答案即可:)
The type of the serialization is determined by an header in your request, or the absence of it. 序列化的类型取决于您请求中的标头或不包含标头。
Content-Type: application/json
Accept: application/json
By default asp.net web api can serialize eiter in xml or json. 默认情况下,asp.net Web API可以将eiter序列化为xml或json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.