.Net Core 2.0替代HttpResponseBase

时间:2017-11-19 11:32:53

标签: c# json http

我正在将一些代码从旧的MVC 5应用程序移植到Core 2.0应用程序。

SerializeData方法失败,因为HttpResponseBase不再在Core 2.0中,我似乎无法在任何Core库中找到合适的替代品。 Resharper甚至没有检测到要作为依赖项添加的正确库。

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);
    }
}

1 个答案:

答案 0 :(得分:2)

参考以下包:

using Microsoft.AspNetCore.Http.HttpResponse;

并将HttpResponseBase更改为HttpResponse

private void SerializeData(HttpResponse response)