11

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

3
  • 1
    What do you mean by "it doesn't work"? That is not descriptive. What are you expecting and what is it actually doing? Commented Nov 7, 2018 at 17:14
  • 1
    Don't use HttpContext or Response like this. ASP.NET Core isn't WebForms and the code used in WebForms isn't needed and won't work. The code posted here doesn't compile or show a complete action. What are you trying to do? Why not return an ActionResult? Commented Nov 7, 2018 at 17:37
  • Read this answer stackoverflow.com/a/40206682 Commented Nov 7, 2018 at 17:42

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How to apply this instructions on .NET Core 3.1? Which files need to repair? Sorry about my English.
There is no specific file to change. It’s just that in .net core, including version 3, setting the response status code is the closest thing there is to ending the request. This is true of any response object, regardless of how you obtain access to it ( via middleware, via a controller, etc) hope that helps make it more clear.
2

Now in August 2022, it is possible to call Response.End through the SystemWebAdapters library. I particularly recommend using it only if you are performing a large-scale migration from a .NET Framework project to .NET Core. If you are in a greenfield project, try not to use this library and write your code following the recommendations for ASP.NET Core request/response lifecycle.

2 Comments

I installed and configured the adapters library, but still no Response.End(), is there anything else that has to be done?
Yes, you need to add .PreBufferRequestStream().BufferResponseStream() in the end of your MapControllerRoute methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.