Skip to content

Commit e72d0d3

Browse files
authored
Merge pull request #239 from Cysharp/hadashiA/expose-media-type
Expose media-type variable of `application/x-memorypack`
2 parents ad5dce5 + 5a924b8 commit e72d0d3

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.Net.Http.Headers;
2+
3+
namespace MemoryPack.AspNetCoreMvcFormatter;
4+
5+
public static class MediaTypeHeaderValues
6+
{
7+
public static readonly MediaTypeHeaderValue ApplicationMemoryPack =
8+
MediaTypeHeaderValue.Parse("application/x-memorypack");
9+
}

src/MemoryPack.AspNetCoreMvcFormatter/MemoryPackInputFormatter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ namespace MemoryPack.AspNetCoreMvcFormatter;
44

55
public class MemoryPackInputFormatter : InputFormatter
66
{
7-
const string ContentType = "application/x-memorypack";
8-
97
public MemoryPackInputFormatter()
108
{
11-
SupportedMediaTypes.Add(ContentType);
9+
SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationMemoryPack);
1210
}
1311

1412
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)

src/MemoryPack.AspNetCoreMvcFormatter/MemoryPackOutputFormatter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using Microsoft.AspNetCore.Mvc.Formatters;
1+
using System.Net.Mime;
2+
using Microsoft.AspNetCore.Mvc.Formatters;
23

34
namespace MemoryPack.AspNetCoreMvcFormatter;
45

56
public class MemoryPackOutputFormatter : OutputFormatter
67
{
7-
const string ContentType = "application/x-memorypack";
88
readonly MemoryPackSerializerOptions? options;
99
readonly bool checkContentType = false;
1010

@@ -17,14 +17,14 @@ public MemoryPackOutputFormatter(bool checkContentType = false)
1717
public MemoryPackOutputFormatter(MemoryPackSerializerOptions options)
1818
{
1919
this.options = options;
20-
SupportedMediaTypes.Add(ContentType);
20+
SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationMemoryPack);
2121
}
2222

2323
public override bool CanWriteResult(OutputFormatterCanWriteContext context)
2424
{
2525
if (checkContentType)
2626
{
27-
return (context.ContentType == ContentType);
27+
return MediaTypeHeaderValues.ApplicationMemoryPack.MatchesMediaType(context.ContentType);
2828
}
2929
else
3030
{
@@ -34,7 +34,7 @@ public override bool CanWriteResult(OutputFormatterCanWriteContext context)
3434

3535
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
3636
{
37-
context.HttpContext.Response.ContentType = ContentType;
37+
context.ContentType = MediaTypeHeaderValues.ApplicationMemoryPack.MediaType;
3838

3939
if (context.Object == null)
4040
{

0 commit comments

Comments
 (0)