Both json And Xml Working for My Api Just Need to Add Few More Nuget Packages Correct Version for The Project Setup Thats All 1. add it to ....
Both json And Xml Working for My Api
Just Need to Add Few More Nuget Packages Correct Version for The Project Setup Thats All
1. add it to .csproj
2. add Some Nuget Packages
1.Microsoft.AspNetCore.Mvc.Formatters.Json
2.Microsoft.AspNetCore.Mvc.Formatters.xml
3.Microsoft.AspNetCore.All (2.2.2)
3. Add On StartUp.cs
services.AddMvc(options =>
{
options.FormatterMappings.SetMediaTypeMappingForFormat
("xml", MediaTypeHeaderValue.Parse("application/xml"));
options.FormatterMappings.SetMediaTypeMappingForFormat
("config", MediaTypeHeaderValue.Parse("application/xml"));
options.FormatterMappings.SetMediaTypeMappingForFormat
("js", MediaTypeHeaderValue.Parse("application/json"));
})
.AddXmlSerializerFormatters();
4.Add On StartUp.CS
using Microsoft.Net.Http.Headers;
5. On top Of Api Controller
[FormatFilter]
[ApiController]
[Route("api/products")]
on getby id Method
[HttpGet("{id}.{format?}"),FormatFilter]
YOu All Done
Now the Default Will be Json And if YOu Want to Use Xml then add ?format=xml
https://andrewlock.net/formatting-response-data-as-xml-or-json-based-on-the-url-in-asp-net-core/
https://youtu.be/yhvLBmwSDNg
COMMENTS