ASP.NET 核心中的 HttpContext 响应如何结束

How HttpContext RESPONSE END in ASP.NET Core

我想使用 mvc System.Web.HttpContext.Current.Response.End(); 但在 mvc core 2 中尝试使用此代码:

 private readonly IHttpContextAccessor _httpContextAccessor;

            public SmsService(IUnitOfWork uow ,IHttpContextAccessor httpContextAccessor) 
            {
                _uow = uow;
                _uow.CheckArgumentIsNull(nameof(_uow));
                _LockIPRequest= uow.Set<LockIPRequest>();
                this._httpContextAccessor = httpContextAccessor;
            }

      _httpContextAccessor.HttpContext.Response.WriteAsync("</body></html>");
      _httpContextAccessor.HttpContext.Response.end();

_httpContextAccessor.HttpContext.Response.end();

结束();不工作 mvc 核心不退出

如评论中所述,Response.End 不存在于 ASP.NET 核心世界。

而不是 Response.End,您应该像这样设置响应状态代码:

 _httpContextAccessor.HttpContext.Response.StatusCode = StatusCodes.Status200OK;

这不会完全 "end" 响应。中间件仍然有机会 运行,但通过设置状态代码,框架可以了解已提供响应。但是,您将负责确保您的下游中间件不会输出其他内容来响应请求。虽然这通常不是问题。