I am porting some code from an old MVC 5 app to a Core 2.0 app.
The SerializeData method is failing as HttpResponseBase is no longer in Core 2.0 and I can't seem to find a suitable replacement in any of the Core libraries. Resharper not even detecting the proper library to add as a dependency.
private void SerializeData(HttpResponseBase response)
{
if (ErrorMessages.Any())
{
Data = new
{
ErrorMessage = string.Join("\n", ErrorMessages),
ErrorMessages = ErrorMessages.ToArray()
};
response.StatusCode = 400;
}
if (Data == null) return;
response.Write(Data.ToJson());
{
}
}
public static class JsonExtensions
{
public static string ToJson<T>(this T obj, bool includeNull = true)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new JsonConverter[] { new StringEnumConverter() },
NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Ignore
};
return JsonConvert.SerializeObject(obj, settings);
}
}
Reference the following package:
using Microsoft.AspNetCore.Http.HttpResponse;
And change HttpResponseBase
to HttpResponse
:
private void SerializeData(HttpResponse response)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With