Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Response.End()?

I am trying to write a piece of middleware to keep certain client routes from being processed on the server. I looked at a lot of custom middleware classes that would short-circuit the response with

context.Response.End();

I do not see the End() method in intellisense. How can I terminate the response and stop executing the http pipeline? Thanks in advance!

public class IgnoreClientRoutes
{
    private readonly RequestDelegate _next;
    private List<string> _baseRoutes;

    //base routes correcpond to Index actions of MVC controllers
    public IgnoreClientRoutes(RequestDelegate next, List<string> baseRoutes) 
    {
        _next = next;
        _baseRoutes = baseRoutes;

    }//ctor


    public async Task Invoke(HttpContext context)
    {
        await Task.Run(() => {

            var path = context.Request.Path;

            foreach (var route in _baseRoutes)
            {
                Regex pattern = new Regex($"({route}).");
                if(pattern.IsMatch(path))
                {
                    //END RESPONSE HERE

                }

            }


        });

        await _next(context);

    }//Invoke()


}//class IgnoreClientRoutes
like image 719
Michael W Riemer Jr Avatar asked Oct 23 '16 18:10

Michael W Riemer Jr


People also ask

What is ASP Net Response end?

The Response. End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response. End is not executed.

What can I use instead of response end?

CompleteRequest Instead of Response. End.

What do you use response end for?

The End method causes the Web server to stop processing the script and return the current result. The remaining contents of the file are not processed.

What is response end in C#?

Sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event. public: void End(); C# Copy.


1 Answers

End does not exist anymore, because the classic ASP.NET pipeline does not exist anymore. The middlewares ARE the pipeline. If you want to stop processing the request at that point, return without calling the next middleware. This will effectively stop the pipeline.

Well, not entirely, because the stack will be unwound and some middlewares could still write some data to the Response, but you get the idea. From your code, you seem to want to avoid further middlewares down the pipeline from executing.

EDIT: Here is how to do it in the code.

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.Use(async (http, next) =>
        {
            if (http.Request.IsHttps)
            {
                // The request will continue if it is secure.
                await next();
            }

            // In the case of HTTP request (not secure), end the pipeline here.
        });

        // ...Define other middlewares here, like MVC.
    }
}
like image 84
gretro Avatar answered Sep 29 '22 11:09

gretro