OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
854 stars 475 forks source link

XML support in aspnet core application #2330

Open MonkAlex opened 3 years ago

MonkAlex commented 3 years ago

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

xuzhg commented 3 years ago

@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.

MonkAlex commented 3 years ago

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>
xuzhg commented 3 years ago

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

vect0r86 commented 3 years ago

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.

Ken-ISC commented 2 years ago

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