5

I am testing OWIN middleware and wrote the following middleware class. But strangely, I see that this middleware is called about four times in total when I request a URL without any path (localhost:<port>). However when I call localhost:<port>/welcome.htm or when I call localhost:<port>/default.htm it is only called once as expected. What am I missing?

public class MyMiddleware : OwinMiddleware
{
    public MyMiddleware(OwinMiddleware next)
        : base(next)
    {
    }

    public override async Task Invoke(IOwinContext context)
    {
        await this.Next.Invoke(context);

        if (context.Request.Path.HasValue && (context.Request.Path.Value == "/" || context.Request.Path.Value.EndsWith(".htm")))
        {
            using (var writer = new StreamWriter(context.Response.Body))
            {
                await writer.WriteLineAsync("Next middleware: " + this.Next.ToString());
            }
        }
    }
}

and here's my Startup configuration:

    public void Configuration(IAppBuilder appBuilder)
    {
        appBuilder.UseErrorPage(new ErrorPageOptions {
            ShowCookies = true,
            ShowEnvironment = true,
            ShowExceptionDetails = true,
            ShowHeaders = true,
            ShowQuery = true,
            ShowSourceCode = true,
            SourceCodeLineCount = 2
        });

        appBuilder.Use<MyMiddleware>();

        appBuilder.UseWelcomePage("/welcome.htm");
        appBuilder.UseStaticFiles();
    }
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
Charles Prakash Dasari
  • 4,964
  • 1
  • 27
  • 46
  • can u inspect HTTP requests through fiddler and post the relevant ones here? – tugberk Oct 23 '14 at 22:59
  • Note: Both calling Next and writing to the response body is discouraged. If someone sets the content-length you could violate it. – Tratcher Oct 29 '14 at 17:40
  • Are these requests coming from the browser? The browser can make additional request for resources like /favicon.ico – Tratcher Oct 29 '14 at 17:41

0 Answers0