ASP.NET Core中的HttpContext如何响应END [英] How HttpContext RESPONSE END in ASP.NET Core
问题描述
我想使用mvc System.Web.HttpContext.Current.Response.End();
但是尝试使用以下代码在mvc core 2中进行操作:
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();不起作用,mvc核心不退出
end(); doesn't work mvc core dont exits
推荐答案
正如注释中所提到的,Response.End在ASP.NET Core世界中不存在.
As has been mentioned in the comments, Response.End does not exist in the ASP.NET Core world.
而不是Response.End,您应该像这样设置响应状态代码:
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.
这篇关于ASP.NET Core中的HttpContext如何响应END的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!