Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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
c#asp.net路由地图中间件_C#_Asp.net - Fatal编程技术网

c#asp.net路由地图中间件

c#asp.net路由地图中间件,c#,asp.net,C#,Asp.net,我正在实现一些中间件,我只想在特定的路由上运行 /api 还有一些其他中间件应该始终运行,一些在特定于api的中间件之前运行,一些在特定于api的中间件之后运行。Startup.cs中的我的代码: app.UseStaticFiles(); app.UseIdentity(); //some more middleware is added here //my own middleware which should only run for routes at /api app.UseCoo

我正在实现一些中间件,我只想在特定的路由上运行

/api
还有一些其他中间件应该始终运行,一些在特定于api的中间件之前运行,一些在特定于api的中间件之后运行。Startup.cs中的我的代码:

app.UseStaticFiles();
app.UseIdentity();
//some more middleware is added here

//my own middleware which should only run for routes at /api
app.UseCookieAuthMiddleware();

//some more middleware is added here
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

app.UseKendo(env);
加载中间件的顺序很重要,所以我想保持这个顺序。
app.UseCookieAuthMiddleware()
只能在以/api开头的URL上运行,例如,
“/api、/api/test、/api?test”
。 我看到app.Map是一个选项,但是如何确保在app.Map之后添加所有其他中间件呢

编辑:地图跳过以下行时的示例:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
   app.UseIdentity();
   //some more middleware is added here
   app.Map("/api", HandleApiRoutes);


   //lines below are skipped ONLY when map is used.
   //some more middleware should be added here 
   app.UseKendo(env);
}


private static void HandleApiRoutes(IApplicationBuilder app)
{
    app.UseCookieAuthMiddleware();
}
我的中间件:

public class CookieCheckMiddleWare
{
    private readonly RequestDelegate _next;

    public CookieCheckMiddleWare(RequestDelegate next)
    {
        _next = next;
    }

    public Task Invoke(HttpContext context)
    {

        Debug.WriteLine("Middleware is called" );

        //Call the next delegate/middleware in the pipeline this._next(context) is called no errors.
        return this._next(context);
    }
}

public static class CookieCheckMiddleWareMiddlewareExtensions
{
    public static IApplicationBuilder UseCookieAuthMiddleware(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<CookieCheckMiddleWare>();
    }
}

但是我想知道如何正确使用地图功能。

我删除了原来的答案并发布了一个新的答案
  public Task Invoke(HttpContext context)
  {
       var url = context.Request.Path.Value.ToString();
       var split = url.Split('/');
       if (split[1] == "api") //should do a extra check to allow /api?something
       {
          //middleware functions
       }
  }