WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

XML support in aspnet core application

Open MonkAlex opened this issue 5 years ago • 5 comments

Want to get xml answer from odata server on aspnet core.

Assemblies affected

Microsoft.OData.Core 7.7.1 Microsoft.AspNetCore.OData 7.5.0

Reproduce steps

I have old project with odata packages:

  <package id="Microsoft.AspNet.OData" version="6.0.0" targetFramework="net48" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.6" targetFramework="net46" />
  <package id="Microsoft.Bcl.AsyncInterfaces" version="1.0.0" targetFramework="net462" />
  <package id="Microsoft.CSharp" version="4.0.1" targetFramework="net46" />
  <package id="Microsoft.Diagnostics.Tracing.EventSource.Redist" version="1.1.28" targetFramework="net46" />
  <package id="Microsoft.Extensions.DependencyInjection" version="1.0.0" targetFramework="net46" />
  <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.0.0" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.JsonWebTokens" version="5.4.0" targetFramework="net462" />
  <package id="Microsoft.IdentityModel.Logging" version="5.4.0" targetFramework="net462" />
  <package id="Microsoft.IdentityModel.Tokens" version="5.4.0" targetFramework="net462" />
  <package id="Microsoft.OData.Core" version="7.7.0" targetFramework="net48" />
  <package id="Microsoft.OData.Edm" version="7.7.0" targetFramework="net48" />

When i send request with Accept header application/xml, server send me xml answer.

Now, i create new project with crossplatform aspnet core, packages:

    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.0" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.2.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.6" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />

Microsoft.AspNetCore.OData references to Microsoft.OData.Core 7.7.1, so i think no critical changes in work.

But when i try to get answer from server in xml format, always get JSON. Try to send application/atom+xml, try to add to request $format=xml, nothing changed, always JSON format.

Expected result

Accept header work as old aspnet.

Actual result

Accept header ignored.

In odata.net repo

https://github.com/OData/odata.net/issues/1908

MonkAlex avatar Oct 22 '20 06:10 MonkAlex

@MonkAlex application/xml only works for xml metadata, all other odata payload, JSON is the only output format. That's the OData v4 spec format.

I am wondering the reason why "When i send request with Accept header application/xml, server send me xml answer.". I do think the result you got is plain XML format, not the OData v3 ATOM format. If you can share part of your xml result, it might be better for us to invesitage.

xuzhg avatar Oct 22 '20 06:10 xuzhg

For first project add this code:

var formatters = ODataMediaTypeFormatters.Create();
config.Formatters.InsertRange(0, formatters);

And result:

<JobTitle xmlns:i="..." xmlns="...">
    <Id>2</Id>
    <Name>SomeName</Name>
    <Status>Active</Status>
</JobTitle>

MonkAlex avatar Oct 22 '20 12:10 MonkAlex

https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2#forcing-a-particular-format

xuzhg avatar Oct 27 '20 16:10 xuzhg

https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2#forcing-a-particular-format

It relatively works, but only by formatting responses in XML. And even in this case response has neither xml standard header nor other atompub structure. As for POST request body - I didn't succeed forcing my OData service to accept body in XML format - it just does not parse it without any failures. I simply got null in my ODataActionParameters.

vect0r86 avatar Oct 30 '20 12:10 vect0r86

Did you find the solution to your problem? cause I'm having the same issue.

Ken-ISC avatar Jun 09 '22 13:06 Ken-ISC