MVC .NET Core Web API XML或JSON [英] MVC .NET Core web API XML or JSON

查看:223
本文介绍了MVC .NET Core Web API XML或JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Web应用程序,该应用程序以XML或JSON的形式返回数据,我该如何做呢?

I would like to create a web application that returns data in the form of XML or JSON, how do I go about doing this?

模型:

namespace ReturningJSONandXML.Models
{
    public class SomeImportantInformation
    {
        public int ID { get; set; }
        public string Information { get; set; }
    }
}

控制器:

namespace ReturningJSONandXML.Controllers
{
    public class GetInfoController : Controller
    {
        // GET: /<controller>/
        public List<SomeImportantInformation> Get()
        {
            List<SomeImportantInformation> ImportantInfo = new List<SomeImportantInformation>();
            ImportantInfo.Add(new SomeImportantInformation { ID = 0, Information = "Awesome info" });
            ImportantInfo.Add(new SomeImportantInformation { ID = 1, Information = "Some other interesting info" });
            return ImportantInfo;
        }
    }
}

我想返回一个XML和JSON文件...

I would like to return an XML and JSON file...

我应该在这里使用的最佳做法是什么?

What are the best practice's I should be using here?

推荐答案

该框架会自动为您处理,因此您无需重新发明轮子.答案在下面引用.但为了简化起见:除非您指定Accept标头,否则API会将响应序列化为JSON.例如,如果您指定"application/xml",它将返回XML.正如MSDN所说:

The framework takes care automatically for you, so you don´t have to reinvent the wheel. The answer is quoted below. But to make it simpler: Unless you specify an Accept header, the API will serialize the response as JSON. If you specify for example 'application/xml' it will return XML. As MSDN Says:

客户端进行内容协商(简称为conneg) 指定一个Accept标头. ASP.NET Core使用的默认格式 MVC是JSON.内容协商由ObjectResult实现.它是 还内置在状态代码中,从中返回特定的操作结果 辅助方法(均基于ObjectResult).你也可以 返回模型类型(您已定义为数据传输的类 类型),框架将自动将其包装在ObjectResult中 为你. 仅当接受标头出现在内容协商中时,才进行内容协商 请求.当请求包含接受标头时,框架 将在首选项中枚举accept标头中的媒体类型 命令,并尝试找到可以在其中产生响应的格式化程序 accept标头指定的一种格式.如果没有 发现可以满足客户要求的格式化程序, 框架将尝试找到第一个可以产生 响应(除非开发人员已在MvcOptions上配置了该选项 返回406不可接受).如果请求指定XML, 但尚未配置XML格式器,则JSON格式器 将会被使用.更一般而言,如果未配置格式化程序,则可以 提供请求的格式,然后是第一个可以格式化的格式器 使用该对象.如果未提供标题,则第一个格式化程序将 可以处理要返回的对象将用于序列化 回复.在这种情况下,不会进行任何协商- 服务器正在确定将使用哪种格式.

Content negotiation (conneg for short) occurs when the client specifies an Accept header. The default format used by ASP.NET Core MVC is JSON. Content negotiation is implemented by ObjectResult. It is also built into the status code specific action results returned from the helper methods (which are all based on ObjectResult). You can also return a model type (a class you've defined as your data transfer type) and the framework will automatically wrap it in an ObjectResult for you. Content negotiation only takes place if an Accept header appears in the request. When a request contains an accept header, the framework will enumerate the media types in the accept header in preference order and will try to find a formatter that can produce a response in one of the formats specified by the accept header. In case no formatter is found that can satisfy the client's request, the framework will try to find the first formatter that can produce a response (unless the developer has configured the option on MvcOptions to return 406 Not Acceptable instead). If the request specifies XML, but the XML formatter has not been configured, then the JSON formatter will be used. More generally, if no formatter is configured that can provide the requested format, then the first formatter than can format the object is used. If no header is given, the first formatter that can handle the object to be returned will be used to serialize the response. In this case, there isn't any negotiation taking place - the server is determining what format it will use.

https://docs.microsoft.com/zh-我们/aspnet/core/mvc/models/formatting

这篇关于MVC .NET Core Web API XML或JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆