【问题标题】:ASP.NET Core Response.End()?ASP.NET Core Response.End()?
【发布时间】:2016-10-23 18:52:30
【问题描述】:

我正在尝试编写一个中间件来防止某些客户端路由在服务器上被处理。我查看了很多自定义中间件类,它们会用

短路响应
context.Response.End();

我没有在智能感知中看到 End() 方法。如何终止响应并停止执行 http 管道?提前致谢!

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

【问题讨论】:

  • 只是不要打电话给next()

标签: c# middleware asp.net-core-1.0


【解决方案1】:

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

嗯,不完全是,因为堆栈将被展开,一些中间件仍然可以将一些数据写入响应,但你明白了。从您的代码来看,您似乎希望避免执行管道中的更多中间件。

编辑:这里是如何在代码中做到这一点。

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】:

    结束方法不再存在。在您的中间件中,如果您在管道中调用 下一个委托,它将转到下一个中​​间件来处理请求并继续,否则它将结束请求。下面的代码展示了一个调用 next.Invoke 方法的示例中间件,如果你省略了like,响应将结束。

    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.");
            }
        }
    }
    

    回到你的代码,你应该简单地从方法中返回,以防模式匹配。

    查看 Microsoft 核心文档中的此文档以了解更多详细信息:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多