【asp.net core】ASP.NET MVC 4 Web API 学习 正确返回JSON数据方法

时间:2019-10-06  来源:ASP.NET MVC  阅读:


web api 本来就可以返回json格式的,为什么我们要还对它做一些修改呢?

因为web api默认返回的是xml,在提交请求的是设置了 Content-Type: application/json;

返回的才是JSON格式的数据!

 但是怎么做到让它只是返回JSON格式的数据呢?

有时候就会遇到这种需求!

 这是我最开始的做法]
public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            config.Formatters.Remove(config.Formatters.XmlFormatter);
}
 

可以返回JSON格式了,但是莫名其妙的多出了个$id 不懂哪里来的!

 

最后正确的办法是使用自定义的只返回Json Result的content negotiation代替Web Api中默认的content negotiation。 Conneg通过实现IContentNegotiator的Negotiator方法实现扩展。Negotiator方法返回ContentNegotiationResult(它包装了你选择的headers和formatter)。下面的方法通过传递一个JsonMediaTypeFormatter给自定义的conneg negotiator,让它一直返回applicaton/json 的content-type以及JsonMediaTypeFormatter。这种方法避免了每次请求都要重新创建一次formatter。

public class JsonContentNegotiator : IContentNegotiator
{
    private readonly JsonMediaTypeFormatter _jsonFormatter;

    public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
    {
        _jsonFormatter = formatter;   
    }

    public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable formatters)
    {
        var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
        return result;
    }
}
 

接下来,你需要在HttpConfiguration实例上注册你的新的实现机制:

var jsonFormatter = new JsonMediaTypeFormatter();
//optional: set serializer settings here
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
 

通过替换默认的DefaultContentNegotiator,我们使用我们自定义的JsonContentNegotiator,它只支持Json,而且可以马上返回。

如果你想更深入的了解Content Negotiation的知识,你可以查看作者的这篇文章。

总结

通过使用自定义的JsonContentNegotiator替换系统默认的DefaultContentNegotiator,很好的实现Web Api只返回Json的功能,而且没有额外的开销。

【asp.net core】ASP.NET MVC 4 Web API 学习 正确返回JSON数据方法

http://m.bbyears.com/asp/71527.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新