首页 > 其他分享 >.net core 接收xml、text/plain格式参数

.net core 接收xml、text/plain格式参数

时间:2023-10-09 14:11:18浏览次数:32  
标签:xml core text plain using 接收 public

1、接收xml

controller中写法如下

[HttpPost, ActionName("Sign_off")] 
[Produces("application/xml")]//接收
[Consumes("application/xml")]//返回
public async Task Sign_off([FromBody] XmlDocument xmldoc){ .....//你的业务逻辑 } 

Startup.cs中的ConfigureServices加上这一段

services.AddMvc().AddXmlSerializerFormatters();

2.接收text/plain

controller中写法如下

[HttpPost, ActionName("Sign_off")] 
[Produces("text/plain")]//接收
[Consumes("application/json")]//返回
public async Task Sign_off([FromBody] string xmlString){ .....//你的业务逻辑 } 

Startup.cs中的ConfigureServices加上这一段

services.AddMvc(options =>
{
options.InputFormatters.Insert(0, new TextInputFormatter());//接收text/plain
});

注:TextInputFormatter是自定义的,如下

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Formatters;

public class TextInputFormatter : InputFormatter
{
    public TextInputFormatter()
    {
        SupportedMediaTypes.Add("text/plain");
    }

    public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
    {
        var request = context.HttpContext.Request;
        using (var reader = new StreamReader(request.Body, Encoding.UTF8))
        {
            var content = await reader.ReadToEndAsync();
            return await InputFormatterResult.SuccessAsync(content);
        }
    }

    protected override bool CanReadType(Type type)
    {
        return type == typeof(string);
    }
}

 

标签:xml,core,text,plain,using,接收,public
From: https://www.cnblogs.com/yyjspace/p/17751587.html

相关文章

  • ASP .Net Core: 使用EF连接postgresql
    备注关于数据库的创建,可参考下方的链接,去创建测试环境,我已经有现成的数据库,故不再记录创建数据库的过程。实现步骤安装EF工具dotnettoolinstall--globaldotnet-ef安装其他依赖dotnetaddpackageNpgsql.EntityFrameworkCore.PostgreSQLdotnetaddpackageMicrosoft.E......
  • Asp-Net-Core开发笔记:EFCore统一实体和属性命名风格
    前言C#编码规范中,类和属性都是大写驼峰命名风格(PascalCase/UpperCamelCase),而在数据库中我们往往使用小写蛇形命名(snake_case),在默认情况下,EFCore会把原始的类名和属性名直接映射到数据库,这不符合数据库的命名规范。为了符合命名规范,而且也为了看起来更舒服,需要自己做命名转换......
  • 造轮子之asp.net core identity
    在前面我们完成了应用最基础的功能支持以及数据库配置,接下来就是我们的用户角色登录等功能了,在asp.netcore中原生Identity可以让我们快速完成这个功能的开发,在.NET8中,asp.netcoreidentity支持了WebApi的注册登录。这让我们在WebApi中可以更爽快的使用。安装包首先我们需要安......
  • NetCore Ocelot 之 Authorization
    Ocelotsupportsclaimsbasedauthorizationwhichisrunpostauthentication.ThismeansifouhavearouteyouwanttoauthorizeyoucanaddthefollowingtoyouRouteconfiguration."RouteClaimsRequirement":{"client_role":......
  • python xml(ElementTree)
    pythonxml处理(ElementTree)1.模块导入fromxml.etree.ElementTreeimportElementTree,Element,SubElement2.对象概述ElementTree:表示整个xml层级结构Element:表示树形结构中的父节点SubElement:表示树形结构中的所有子节点,有些节点既可以是父节点,也可以是子节点3.Elem......
  • 【Azure Logic App】在Logic App中使用 Transfer XML组件遇见错误 undefined
    问题描述在AzureLogicApp中,使用TransformXML组件进行XML内容的转换,但是最近这个组件运行始终失败。 问题解答点击TransformXML组件上的错误案例,并不能查看到详细的错误消息。最后在AzureLogicApp的产品团队确认下,发现这是LogicApp自身升级后,当前LogicApp 依旧是旧版所引......
  • 【Azure Logic App】在Logic App中使用 Transfer XML组件遇见错误 undefined
    问题描述在AzureLogicApp中,使用TransformXML组件进行XML内容的转换,但是最近这个组件运行始终失败。 问题解答点击TransformXML组件上的错误案例,并不能查看到详细的错误消息。最后在AzureLogicApp的产品团队确认下,发现这是LogicApp自身升级后,当前LogicApp 依旧是......
  • NetCore Ocelot 之 Qos
    QosqualityofserviceOcelotsupportsoneQoscapabilityatthecurrenttime.YoucansetonaperRoutebasisifyouwanttouseacircuitbreakerwhenmakingrequeststoadownstreamservice.Thisusesanawesome.NETlibrarycalledPolly.Thefirstthi......
  • NetCore Ocelot 之 Load Balancer
    OcelotcanloadbalanceacrossavailabledownstreamservicesforeachRoute.ThismeansyoucanscaleyourdownstreamservicesandOcelotcanusethemeffectively.TheTypeofloadbalanceravailbleare:  LeastConnection -trackswhichservicearedeal......
  • NetCore Ocelot 之 Authentication
    InordertoauthenticateRoutesandsubsequentlyuseanyofOcelot'sclaimsbasedfeaturessuchasauthorizationormodifyingtherequestwithvaluesfromthetoken.UsersmustregisterauthenticationservicesintheirStartup.csasusualbuttheypr......