ASP.NET Core Response.End()?

谁说我不能喝 提交于 2020-01-23 04:55:16

问题


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

回答1:


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.
    }
}



回答2:


End method is not there anymore. In your middleware, if you invoke the next delegate in the pipeline it would go to the next middleware to handle the request and proceed, otherwise it would end the request. The following code shows a sample middleware which calls the next.Invoke method, if you omit that like, the response will end.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace MiddlewareSample
{
    public class RequestLoggerMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger _logger;

        public RequestLoggerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
        {
            _next = next;
            _logger = loggerFactory.CreateLogger<RequestLoggerMiddleware>();
        }

        public async Task Invoke(HttpContext context)
        {
            _logger.LogInformation("Handling request: " + context.Request.Path);
            await _next.Invoke(context);
            _logger.LogInformation("Finished handling request.");
        }
    }
}

Getting back to your code you should simply return from the method in case of the pattern match.

Take a look into this doc from Microsoft core docs for more details: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware



来源:https://stackoverflow.com/questions/40206633/asp-net-core-response-end

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