Skip to content

Commit

Permalink
Update to fix xml issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnwildermuth committed Jan 26, 2024
1 parent e6d383c commit b4521d4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Apis/ProjectsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace RestDesign.Apis;

public class ProjectsApi : IApi
{
const string XmlContentType = "application/xml";
static readonly string[] XmlContentTypes = { "application/xml", "text/xml" };

public void Register(IEndpointRouteBuilder builder)
{
Expand All @@ -42,14 +42,15 @@ public static async Task<IResult> GetAll(HttpContext httpContext, BillingContext
.OrderBy(e => e.ProjectName)
.ToListAsync();

if (httpContext.Request.Headers.Accept.Contains(XmlContentType))
var contentType = httpContext.Request.Headers.Accept.FirstOrDefault(h => XmlContentTypes.Contains(h));
if (contentType is not null)
{
var serializer = new XmlSerializer(result.GetType());
using var writer = new StringWriter();
serializer.Serialize(writer, result);
var xml = writer.ToString();

httpContext.Response.ContentType = XmlContentType;
httpContext.Response.ContentType = contentType;
await httpContext.Response.WriteAsync(xml);
return Results.Ok();
}
Expand Down

0 comments on commit b4521d4

Please sign in to comment.