Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 2.0 replacement for HttpResponseBase

Tags:

json

c#

http

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);
    }
}
like image 787
dinotom Avatar asked Nov 19 '17 11:11

dinotom


1 Answers

Reference the following package:

using Microsoft.AspNetCore.Http.HttpResponse;

And change HttpResponseBase to HttpResponse:

private void SerializeData(HttpResponse response)
like image 116
ceferrari Avatar answered Oct 21 '22 05:10

ceferrari