Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 中间件在启动项目时写入响应_Asp.net Core_Swagger_Middleware - Fatal编程技术网

Asp.net core 中间件在启动项目时写入响应

Asp.net core 中间件在启动项目时写入响应,asp.net-core,swagger,middleware,Asp.net Core,Swagger,Middleware,使用ASP.Net内核,C# 我有一个中间件,我检查特定的cookie是否存在,否则返回400响应。我的问题是中间件启动项目本身并检查cookie是否存在,然后在swagger索引页中显示响应文本,这是我不希望看到的。当swagger加载时,中间件会激活。我希望仅对请求执行此条件 public async Task InvokeAsync(HttpContext context) { var pl = context.Request.Cookies[&qu

使用ASP.Net内核,C#

我有一个中间件,我检查特定的cookie是否存在,否则返回400响应。我的问题是中间件启动项目本身并检查cookie是否存在,然后在swagger索引页中显示响应文本,这是我不希望看到的。当swagger加载时,中间件会激活。我希望仅对请求执行此条件

public async Task InvokeAsync(HttpContext context)
        {
            var pl = context.Request.Cookies["pl"];
            var sig = context.Request.Cookies["sig"];
            if (string.IsNullOrWhiteSpace(pl) || string.IsNullOrWhiteSpace(sig))
            {
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync("Invalid Data");
                return;
            }
           // If success i process and do something
            
            // Call the next delegate/middleware in the pipeline
            await _next(context);
        }

我可以防止在swagger加载时启动中间件,并且仅针对api请求启动中间件。但这是更好的方法还是其他更好的方法

app.UseWhen(context => `context.Request.Path.ToString().Contains("/api"),HandleBranch);`