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
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.
CompleteRequest Instead of Response. End.
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.
Sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event. public: void End(); C# Copy.
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.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With