简体   繁体   English

ASP.NET Core Response.End()?

[英]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. 我没有在intellisense中看到End()方法。 How can I terminate the response and stop executing the http pipeline? 如何终止响应并停止执行http管道? 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

End does not exist anymore, because the classic ASP.NET pipeline does not exist anymore. End不再存在,因为经典的ASP.NET管道不再存在。 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. 嗯,不完全是因为堆栈将被解开,一些中间件仍然可以将一些数据写入Response,但是你明白了。 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.
    }
}

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. 下面的代码显示了一个调用next.Invoke方法的示例中间件,如果省略,则响应将结束。

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 有关详细信息,请查看Microsoft核心文档中的此文档: https//docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM