Skip to content

Commit 082cff9

Browse files
committed
Small bugfix in the formatters.
1 parent 1017807 commit 082cff9

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

Mvc.Datatables/Formatters/DatatablesMediaTypeFormatter.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public DatatablesMediaTypeFormatter()
2525
this.MediaTypeMappings.Clear();
2626
this.AddRequestHeaderMapping("x-requested-with", "XMLHttpRequest", StringComparison.OrdinalIgnoreCase, true, ApplicationDatatablesMediaType);
2727

28-
this.SerializerSettings.Converters.Add(new FilterRequestConverter());
29-
this.SerializerSettings.Converters.Add(new PageResponseConverter());
28+
this.SerializerSettings.Converters.Add(new FilterRequestConverter());
29+
this.SerializerSettings.Converters.Add(new PageResponseConverter());
3030

31-
this.SerializerSettings.Formatting = Formatting.Indented;
31+
this.SerializerSettings.Formatting = Formatting.Indented;
3232
}
3333

3434
public new static MediaTypeHeaderValue DefaultMediaType
@@ -46,12 +46,20 @@ public override void SetDefaultContentHeaders(Type type, HttpContentHeaders head
4646

4747
public override bool CanReadType(Type type)
4848
{
49-
return true;
49+
foreach (var converter in this.SerializerSettings.Converters)
50+
if (converter.CanConvert(type))
51+
return true;
52+
53+
return false;
5054
}
5155

5256
public override bool CanWriteType(Type type)
5357
{
54-
return true;
58+
foreach (var converter in this.SerializerSettings.Converters)
59+
if (converter.CanConvert(type))
60+
return true;
61+
62+
return false;
5563
}
5664

5765
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)

Mvc.Datatables/Formatters/LegacyDatatablesMediaTypeFormatter.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class LegacyDatatablesMediaTypeFormatter : JsonMediaTypeFormatter
1515
public static readonly string ApplicationDatatablesMediaType = "application/datatables";
1616
public static readonly string TextDatatablesMediaType = "text/datatables";
1717

18-
public LegacyDatatablesMediaTypeFormatter()
18+
public LegacyDatatablesMediaTypeFormatter()
1919
: base()
2020
{
2121
this.SupportedMediaTypes.Clear();
@@ -25,10 +25,10 @@ public LegacyDatatablesMediaTypeFormatter()
2525
this.MediaTypeMappings.Clear();
2626
this.AddRequestHeaderMapping("x-requested-with", "XMLHttpRequest", StringComparison.OrdinalIgnoreCase, true, ApplicationDatatablesMediaType);
2727

28-
this.SerializerSettings.Converters.Add(new LegacyFilterRequestConverter());
29-
this.SerializerSettings.Converters.Add(new LegacyPageResponseConverter());
28+
this.SerializerSettings.Converters.Add(new LegacyFilterRequestConverter());
29+
this.SerializerSettings.Converters.Add(new LegacyPageResponseConverter());
3030

31-
this.SerializerSettings.Formatting = Formatting.Indented;
31+
this.SerializerSettings.Formatting = Formatting.Indented;
3232
}
3333

3434
public new static MediaTypeHeaderValue DefaultMediaType
@@ -46,12 +46,20 @@ public override void SetDefaultContentHeaders(Type type, HttpContentHeaders head
4646

4747
public override bool CanReadType(Type type)
4848
{
49-
return true;
49+
foreach (var converter in this.SerializerSettings.Converters)
50+
if (converter.CanConvert(type))
51+
return true;
52+
53+
return false;
5054
}
5155

5256
public override bool CanWriteType(Type type)
5357
{
54-
return true;
58+
foreach (var converter in this.SerializerSettings.Converters)
59+
if (converter.CanConvert(type))
60+
return true;
61+
62+
return false;
5563
}
5664

5765
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)

0 commit comments

Comments
 (0)