How HttpContext RESPONSE END in ASP.NET Core

拟墨画扇 提交于 2019-12-23 02:53:16

问题


I want use mvc System.Web.HttpContext.Current.Response.End(); but trying in mvc core 2 with this code:

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

end(); doesn't work mvc core dont exits


回答1:


As has been mentioned in the comments, Response.End does not exist in the ASP.NET Core world.

Instead of Response.End, you should set the response status code like so:

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

This won't fully "end" the response. The middleware still gets a chance to run, but by setting the status code, the framework has a way to understand that a response has been provided. You will however be responsible for making sure that your downstream middleware does not output other content in response to the request. Although this is often not a problem.



来源:https://stackoverflow.com/questions/53194200/how-httpcontext-response-end-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!