How to stream with ASP.NET Core

后端 未结 6 948
借酒劲吻你
借酒劲吻你 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 06:54

    It is possible to return null or EmptyResult() (which are equivalent), even when previously writing to Response.Body. It may be useful if the method returns ActionResult to be able to use all the other results aswell (e.g. BadQuery()) easily.

    [HttpGet("test")]
    public ActionResult Test()
    {
        Response.StatusCode = 200;
        Response.ContentType = "text/plain";
        using (var sw = new StreamWriter(Response.Body))
        {
            sw.Write("something");
        }
        return null;
    }
    

提交回复
热议问题