您现在的位置是:主页 > news > 房地产门户网站/坚持
房地产门户网站/坚持
admin2025/5/2 0:58:46【news】
简介房地产门户网站,坚持,铜陵网站建设费用,电子商务平台网店关键词挖掘方法asp中格式化输出函数ASP.Net Core为JSON和XML格式的数据交换提供内置支持。 如果您希望ASP.Net Core应用程序通过HTTP传输这些内置格式器之一不支持的格式的数据,则需要创建一个自定义输出格式器。 例如,如果您想使用更高效的Protobuf格式与客户端交换数…
asp中格式化输出函数
ASP.Net Core为JSON和XML格式的数据交换提供内置支持。 如果您希望ASP.Net Core应用程序通过HTTP传输这些内置格式器之一不支持的格式的数据,则需要创建一个自定义输出格式器。
例如,如果您想使用更高效的Protobuf格式与客户端交换数据,则可能要构建自定义输出格式化程序。 本文讨论了什么是输出格式化程序,为什么需要它们以及如何在ASP.Net Core中构建自定义输出格式化程序。
创建一个ASP.Net Core Web API项目
首先,让我们在Visual Studio中创建一个ASP.Net Core Web API项目。 如果您的系统已启动并运行Visual Studio 2017,请按照下面给出的步骤创建一个ASP.Net Core Web API项目。
- 启动Visual Studio 2017 IDE。
- 单击文件>新建>项目。
- 从显示的模板列表中选择“ ASP.Net Core Web应用程序(.Net Core)”。
- 指定项目的名称。
- 单击确定保存项目。
- 接下来显示一个新窗口“ New .Net Core Web Application…”。
- 选择.Net Core作为运行时,并从窗口顶部的下拉列表中选择ASP.Net Core 2.1或更高版本。
- 选择“ API”作为项目模板。
- 确保未选中“启用Docker支持”和“配置HTTPS”复选框,因为我们在这里不使用这些功能。
- 确保选择“无身份验证”,因为我们也不会使用身份验证。
这将在Visual Studio中创建一个新的ASP.Net Core项目。 我们将使用该项目来处理输出格式化程序。
什么是输出格式化程序? 为什么需要它?
输出格式化程序是一个类,可以将要通过网络发送的数据转换为其他格式。 因此,如果您希望ASP.Net Core Web API能够通过HTTP以Excel格式(.xlsx)发送数据,则需要为其构建自定义输出格式化程序。
客户端利用内容协商来确定接收数据的格式,并在Accept-Header中指定内容类型。 请注意,内容协商是检查传入HTTP请求的结构以确定资源的多种表示形式中最佳资源表示形式的过程。 内容协商允许您使用相同的URL为不同的客户端提供不同格式的相同内容。
请注意,在ASP.Net Core Web API中,即使在标题中将text/html
指定为内容类型,也始终以JSON形式返回数据。 这是因为默认情况下未注册内置XML格式器。 现在,假设客户端指定了一种其要接收数据的格式,但是该格式不可用。 在这种情况下,框架将使用默认的JSON格式器作为输出格式器。
以下代码段显示了如何注册XML格式化程序,以使ASP.Net Core Web API能够以text/xml
格式发射数据。
services.AddMvc(options =>
{
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
如果要将XML用作输入和输出格式化程序,则可以在Startup类的ConfigureServices方法中指定以下内容。
options.InputFormatters.Add(new XmlSerializerInputFormatter());
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
在ASP.Net Core中创建自定义输出格式化程序
要创建自定义输出格式化程序,您应该创建一个扩展任何可用输出格式化程序(TextOutputFormatter,JsonOutputFormatter等)或OutputFormatter类的类。 顺便说一句,OutputFormatter是所有输出格式化程序的基类。 同样,InputFormatter是所有输入格式化程序的基类。 格式化程序TextInputFormatters,JsonInputFormatter等都扩展了InputFormatter类。
要创建一个简单的输出格式化程序,请创建一个扩展TextOutputFormatter类并重写WriteResponseBodyAsync方法的类,如下所示。
public class MyCustomOutputFormatter : TextOutputFormatter
{
public override Task WriteResponseBodyAsync
(OutputFormatterWriteContext context,
Encoding selectedEncoding)
{
throw new NotImplementedException();
}
}
接下来,您需要编写必要的代码以指定受支持的媒体类型和受支持的编码。 您可以在MyCustomOutputFormatter类的构造函数中编写此代码,如下面给出的代码片段所示。
public MyCustomOutputFormatter()
{
SupportedMediaTypes.Add
(MediaTypeHeaderValue.Parse(""));
SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
//Write code here to add the supported media types
}
下一步是重写CanWriteType方法,以指定如何序列化数据。 这是MyCustomOutputFormatter类现在的外观。
using Microsoft.AspNetCore.Mvc.Formatters;
using System;
using Microsoft.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace IDGExamples
{
public class MyCustomOutputFormatter : TextOutputFormatter
{
public MyCustomOutputFormatter()
{
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse
("text/MyCustomFormat"));
SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
}
public bool CanWriteType
(OutputFormatterCanWriteContext context)
{
throw new NotImplementedException();
}
public override Task WriteResponseBodyAsync
(OutputFormatterWriteContext context,
Encoding selectedEncoding)
{
throw new NotImplementedException();
}
}
}
我将留给您来实现CanWriteType和WriteResponseBodyAsync方法。
在ASP.Net Core中注册自定义输出格式化程序
注册格式化程序时,可以将格式化程序添加到FormatterCollection中。 在运行时,当请求中包含一个Accept标头时,ASP.Net Core框架将迭代此集合,以检索Accept标头中指定的格式程序。
您可以在ConfigureServices方法中注册自定义输出格式器,如下面的代码片段所示。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.OutputFormatters.Add(new
MyCustomOutputFormatter());
//Usual code
});
}
ASP.Net Core提供对JSON,XML和纯文本格式的内置支持,以便通过网络发送数据。 每当您想以其他格式传输数据时,都需要创建一个自定义输出格式化程序。
翻译自: https://www.infoworld.com/article/3356437/how-to-use-output-formatters-in-aspnet-core.html
asp中格式化输出函数