Search code examples
c#asp.netowinkatana

Owin IAppBuilder.Map unexpectedly handling all requests


string path = "/SomeSubPath";
            app.Map(new PathString(path),
                (application) =>
                {
                    app.Run((ctx) =>
                    {
                        Debug.WriteLine("{0} : {1}", path, ctx.Request.Path);

                        return Task.FromResult(0);
                    });
                });

            app.Use(async (ctx, next) =>
            {
                var watch = new Stopwatch();

                watch.Start();
                await next();
                watch.Stop();
                Debug.WriteLine("Request handled in {0} [ms]", watch.ElapsedMilliseconds);
            });

            app.Use(async (ctx, next) =>
            {
                Debug.WriteLine("{0} : {1}", "/", ctx.Request.Path);
                await next();
            });

Consider the above pipeline configuration.

In my startup class, I'd like to map "SomeSubPath" to an exclusive middleware handler. Ie. Requests starting with "/SomeSubPath" should be handled by some exclusive middleware, while all other requests should be handled by the rest of the pipeline. I'm using the IAppBuilder.Map extension as found here. To my surprise, the middleware is intercepting ALL requests, suppressing the rest of the application from processing those requests.

  • Why is this happening?
  • How can I get this right?

Solution

  • Change this block like this. Inside Map, you are using app instead of application.

           app.Map(new PathString(path),
            (application) =>
            {
                application.Run((ctx) =>
                {
                    Debug.WriteLine("{0} : {1}", path, ctx.Request.Path);
    
                    return Task.FromResult(0);
                });
            });