Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Asp.NETWebAPI2获得类似于Google Api响应的JSON结果?_C#_Json_Asp.net Mvc 5_Asp.net Web Api2 - Fatal编程技术网

C# 如何从Asp.NETWebAPI2获得类似于Google Api响应的JSON结果?

C# 如何从Asp.NETWebAPI2获得类似于Google Api响应的JSON结果?,c#,json,asp.net-mvc-5,asp.net-web-api2,C#,Json,Asp.net Mvc 5,Asp.net Web Api2,使用ASP.net Web api 2如何以类似google的方式响应: 我的模型: [DataContract] public class ProductResponse { [DataMember] public string Status_Message { get; set; } [DataMember] public List<Product> Products { get; set; } } [DataContract] 公共类产品响

使用ASP.net Web api 2如何以类似google的方式响应:

我的模型:

[DataContract]
public class ProductResponse
{
    [DataMember]
    public string Status_Message { get; set; }

    [DataMember]
    public List<Product> Products { get; set; }
}
[DataContract]
公共类产品响应
{
[数据成员]
公共字符串状态_消息{get;set;}
[数据成员]
公共列表产品{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;
    }
公共产品响应GetAllProducts() { ProductResponse=新的ProductResponse(); 尝试 { 使用(SQLServerWrapper sqlConn=newSQLServerWrapper()) { 使用(SqlCommand=sqlConn.PrepareCommand(Product.SQLSelect)) { SqlDataReader=command.ExecuteReader(); response.Products=reader.Select(Product.FromDataReader.ToList(); } } response.Status=“OK”; } 捕获(例外情况除外) { response.Status=“ERROR”; } 返回响应; }
一切都很好。我的问题是如何将响应切换到浏览器可读的JSON类型?

简单,只需请求JSON的回答:)

序列化的类型由请求中的头或不存在头决定

Content-Type: application/json
Accept: application/json

默认情况下,asp.net web api可以用xml或json序列化eiter。

请参阅,其中的共识是“不,他们不应该”!请继续写标题——目前它只是一系列术语,与标题本身没有任何关系。你说的“如何将响应转换为浏览器可读的JSON类型”是什么意思?浏览器不读取JSON。您的产品和Google Maps的地理代码输出之间有什么关系?只需返回json fortmat格式的结果。所谓“浏览器可读的json类型”,我指的是由浏览器插件(如Firefox JSONViewHello)正确解释的格式,您解决了这个问题吗?怎么用?