English 中文(简体)
有可能在伙伴关系中进行。 NET 单一控制器实现XmlSerial化的核心
原标题:Is it possible in ASP.NET Core to enable XmlSerialization for a single controller

我有一个有几名控制员的网络信息预报系统,其中一人由于遗留原因返回了XML,而其他所有的人都返回了JSON。

在“网络框架一”中,可以有选择地通过使用<代码>HttpControllerSettings.Formatters(通常用一种特性来分解控制器)。

Is it possible to do the same in ASP.NET Core, i.e. only enable Xml serialization for a single controller?

我发现的唯一途径是使协会的Xml序列化。 NET Core 2.1 isglobal, using:

services.AddMvc()
     .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
     .AddXmlSerializerFormatters();

这一工作是科索沃的,但我认为是不可取的副作用:在我管理视觉演播室一号应用时,见<密码>的警告痕迹。 XmlSerializerOutputFormatter 例如:

Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter:Warning: An error occurred while trying to create an XmlSerializer for the type  MyApp.MyModel .

System.InvalidOperationException: There was an error reflecting type  MyApp.MyModel . ---> System.InvalidOperationException: Cannot serialize member ... see inner exception for more details. ---> System.NotSupportedException: Cannot serialize ... because it is an interface.
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type)
   ...
   at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type)
   at Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter.CreateSerializer(Type type)

它似乎试图为我的“XML”控制器没有使用的类型建立一个<代码>XmlSerializer。

虽然这不是一个 shows子,但我更希望不要制造XmlSerializer,除非我需要。

问题回答

我也有同样的问题。

通过确定生产者归属于引起这一问题的行动/json。

[HttpGet]
[Route("yourroute")]
[Produces("application/json")]
public async Task<IActionResult<Whatever> GetSomething() {

没有一种“盒式”的方式来做到这一点,没有人工序列化,也没有通过<代码>FileContentRes或类似方式返回。 但是,你可以增加一个“标识”类别和格式,专门处理标识类别。

Steps are:

  1. Define a marker wrapper struct which wraps the value to be serialized/deserialized.
    (Example Implementation)
  2. Define a TextInputFormatter/TextOutputFormatter which read/write your marker class using XmlSerializable.
    (MSDN Guidance, Example Input Implementation, Example Output Implementation)
  3. Register the formatters earlier in the list than DCS in MvcOptions.
    (Example Implementation)
  4. Optionally: Add a JsonConverter to the marker class to make it invisible to JSON

An implementation of this is available in Rmg.AspNetCore.ByXmlSerializer (Nuget, MIT Licensed). Example use:

// Add Nuget reference to Rmg.AspNetCore.ByXmlSerializer

// in Program.cs
builder.Services.AddControllers()
    .AddByXmlSerializerFormatters()
    .AddXmlDataContractSerializerFormatters();

// in controller
[HttpGet("xs")]
public ByXmlSerializer<WeatherForecast[]> GetWeatherForecastXmlSerializer()
{
    WeatherForecast[] result = /* ... */;
    return result;
}

// or for input
[HttpPost("postxs")]
public void PostWeatherForecastXs([FromBody] ByXmlSerializer<ForecastArea> area)
{
    // ...
}




相关问题
asp.net 6 c# mvc one to many get data from many side

I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with ...

How to update shared view adminheader.cshtml from a model

I ve added a drop down into my shared adminheader.cshtl view and it needs to be dynamically updated from data from a table. So when I use another view like routeplan.cshtml I load the view from ...

Gitlab CI On Prem, Docker Image and ASP.NET Core 7

We have a .NET 6 application. We added CI using: image: mcr.microsoft.com/dotnet/sdk:6.0 before_script: - dotnet restore --packages $NUGET_PACKAGES_DIRECTORY build: stage: build script: - ...

ASP.NET Core 2 .1 - How to show all data in table

I don t know how to put CustomerData into CustomerLists property. I m already using CustomerLists = CustomerData; but I got error: CustomerLists is a type but is used like a variable Can anyone ...

热门标签