How to stream with ASP.NET Core

后端 未结 6 965
借酒劲吻你
借酒劲吻你 2020-12-24 06:17

How to properly stream response in ASP.NET Core? There is a controller like this (UPDATED CODE):

[HttpGet(\"test\")]
public async Task GetT         


        
6条回答
  •  忘掉有多难
    2020-12-24 07:14

    I was wondering as well how to do this, and have found out that the original question's code actually works OK on ASP.NET Core 2.1.0-rc1-final, neither Chrome (and few other browsers) nor JavaScript application do not fail with such endpoint.

    Minor things I would like to add are just set StatusCode and close the response Stream to make the response fulfilled:

    [HttpGet("test")]
    public void Test()
    {
        Response.StatusCode = 200;
        Response.ContentType = "text/plain";
        using (Response.Body)
        {
            using (var sw = new StreamWriter(Response.Body))
            {
                sw.Write("Hi there!");
            }
        }
    }
    

提交回复
热议问题