ASP.NET Core Response.End()? [英] ASP.NET Core Response.End()?

查看:1154
本文介绍了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();

在智能感知中我看不到End()方法.如何终止响应并停止执行http管道?预先感谢!

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

推荐答案

End不再存在,因为经典的ASP.NET管道已不存在.中间件是管道.如果您想在此时停止处理请求,请返回而不调用下一个中间件.这将有效地停止管道.

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.

嗯,不是全部,因为将取消堆栈的堆栈,并且某些中间件仍可以向Response写入一些数据,但是您明白了.从您的代码看来,您似乎想避免执行进一步的中间件.

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.

这是在代码中的方法.

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

这篇关于ASP.NET Core Response.End()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆