简体   繁体   English

如何从Asp.Net Web Api 2获得类似于Google API响应的JSON结果?

[英]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.

相关问题 如何从 ASP.NET Core Web API 向客户端发送 JSON 响应? - How can I send JSON response to client from ASP.NET Core Web API? 无法从ASP.NET Web API获得响应 - Can't get response from ASP.NET web API JSON:我得到的不是{{result“:true}而是true(asp.net web api) - JSON: Instead of {“result”:true} i get just true ( asp.net web api ) 如何在 ASP.NET Core 6 Web API 中以像素形式获取图像内容并从中创建图像? - How can I get image content as pixel and create image from it in ASP.NET Core 6 Web API? 如何从asp.net Web API 使用webApi 将结果存储在json 文件中 - How to consume a webApi from asp.net Web API to store result in json file 无法从 ASP.NET Web API 获得任何响应 - Can't get any response from ASP.NET Web API 如何在ASP.NET Web Api中配置gzip压缩响应? - How can I configure a gzip compressed response in ASP.NET Web Api? Sending class data as JSON array format for GET request Response in ASP.Net Dot Core Web API ( GET response data from Web API) - Sending class data as JSON array format for GET request Response in ASP.Net Dot Core Web API ( GET response data from Web API) 如何使用ASP.NET MVC4和Web API创建客户api以获取预定义的json结果 - How to create customers api using ASP.NET MVC4 and Web API for predefined json result ASP.NET web api 2 截断 json 响应 - ASP.NET web api 2 truncating json response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM