You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.4 KiB
41 lines
1.4 KiB
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
using System.Xml.Serialization;
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
using Microsoft.AspNetCore.Mvc.Formatters;
|
|
|
|
namespace Win_in.Sfs.Scp.WebApi.XmlHost
|
|
{
|
|
public class XDocumentInputFormatter : InputFormatter, IInputFormatter, IApiRequestFormatMetadataProvider
|
|
{
|
|
private Type currentType { get; set; }
|
|
public XDocumentInputFormatter()
|
|
{
|
|
SupportedMediaTypes.Add("application/xml");
|
|
}
|
|
|
|
protected override bool CanReadType(Type type)
|
|
{
|
|
currentType = type;
|
|
if (type.IsAssignableFrom(typeof(XDocument))) return true;
|
|
return base.CanReadType(type);
|
|
}
|
|
|
|
|
|
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
|
|
{
|
|
// Use StreamReader to convert any encoding to UTF-16 (default C# and sql Server).
|
|
using var streamReader = new StreamReader(context.HttpContext.Request.Body);
|
|
var xmlDoc = await XDocument.LoadAsync(streamReader, LoadOptions.None, CancellationToken.None);
|
|
var model = new XmlSerializer(currentType).Deserialize(xmlDoc.CreateReader());
|
|
return await InputFormatterResult.SuccessAsync(model);
|
|
}
|
|
|
|
}
|
|
|
|
}
|