简体   繁体   English

将类实现从asp.net mvc移植到asp.net mvc核心

[英]Porting a class implementation from asp.net mvc to asp.net mvc core

I was trying to implement a subclass of ActionResult that will stream big JSON objects from a REST API, I found this solution on stack overflow but it seems like it's an implementation for asp.net MVC. 我试图实现ActionResult的子类,该子类将通过REST API流式传输大的JSON对象,我在堆栈溢出时发现了该解决方案,但似乎是asp.net MVC的实现。

public class JsonStreamingResult : ActionResult
{
    private IEnumerable itemsToSerialize;

    public JsonStreamingResult(IEnumerable itemsToSerialize)
    {
        this.itemsToSerialize = itemsToSerialize;
    }

    public override void ExecuteResult(ActionContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "application/json";
        response.ContentEncoding = Encoding.UTF8;

        JsonSerializer serializer = new JsonSerializer();

        using (StreamWriter sw = new StreamWriter(response.OutputStream))
        using (JsonTextWriter writer = new JsonTextWriter(sw))
        {
            writer.WriteStartArray();
            foreach (object item in itemsToSerialize)
            {
                JObject obj = JObject.FromObject(item, serializer);
                obj.WriteTo(writer);
                writer.Flush();
            }
            writer.WriteEndArray();
        }
    }
}

But when I was in the process of porting this to asp.net core MVC I found that the response class does not have ContentEncoding and OutputStream properties. 但是,当我将其移植到asp.net核心MVC的过程中,我发现响应类没有ContentEncodingOutputStream属性。

Please, can anyone provide the required changes to port this class to asp.net core? 请,有人可以提供所需的更改以将该类移植到asp.net core吗?

Thanks in advance. 提前致谢。

OutputStream - in ASP.NET Core HttpResponse contains Body property to which you can write a response. OutputStream-ASP.NET Core HttpResponse包含可以向其写入响应的Body属性。 ContentEncoding - set encoding for StreamWriter since you manually write result to the response stream. ContentEncoding-设置StreamWriter编码,因为您将结果手动写入响应流。 In ASP.NET MVC HttpResponse.ContentEncoding were only used when you called HttpResponse.Write methods. ASP.NET MVC HttpResponse.ContentEncoding当你打电话只用于HttpResponse.Write方法。

public class JsonStreamingResult : ActionResult
{
    private IEnumerable itemsToSerialize;

    public JsonStreamingResult(IEnumerable itemsToSerialize)
    {
        this.itemsToSerialize = itemsToSerialize;
    }

    public override void ExecuteResult(ActionContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "application/json";

        JsonSerializer serializer = new JsonSerializer();

        using (StreamWriter sw = new StreamWriter(response.Body, Encoding.UTF8))
        using (JsonTextWriter writer = new JsonTextWriter(sw))
        {
            writer.WriteStartArray();
            foreach (object item in itemsToSerialize)
            {
                JObject obj = JObject.FromObject(item, serializer);
                obj.WriteTo(writer);
                writer.Flush();
            }
            writer.WriteEndArray();
        }
    }
}

Update 更新资料

According to source code , JsonResultExecutor internally does exactly what I've described, only difference is it parses encoding from ContentType . 根据源代码JsonResultExecutor内部完全按照我的描述进行操作,只是区别在于它解析ContentType编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM