locked
DelegatingHandler added in OWIN Startup class fires for both MVC and Web API routes RRS feed

  • Question

  • User272637413 posted

    Adding a DelagatingHandler implementation in an OWIN Startup class causes the handler to be invoked for both MVC and Web API route requests. My understanding is that DelegatingHandlers should only be invoked for routes to controllers that extend ApiController.

    It's easy to reproduce

    • Create a new web app using the ASP.NET MVC template
    • Install the Microsoft.AspNet.WebApi.Owin and Microsoft.Owin.Host.SystemWeb NuGet packages
    • Move Web API startup from WebApiConfig.cs to a new Startup.cs file
    • Create a DelegatingHandler and add it to config.MessageHandlers in the Startup class
    • The DelegatingHandler will be invoked for MVC and API requests

    If I add the DelegatingHandler in WebApiConfig it behaves as expected. Is this a bug?

    Friday, April 15, 2016 7:58 PM

Answers

  • User272637413 posted

    Thanks for your reply. I don't think I'm using anything that is in beta and a DelgatingHandler is a message handler.

    I tried several combinations of startup code before I found a workaround. In the handler, I am checking if the route starts with "/api/" and only wrapping the response if it does.

        public class WtfDelegatingHandler : DelegatingHandler
        {
            protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                var response = await base.SendAsync(request, cancellationToken);
    
                if (request.RequestUri.LocalPath.StartsWith("/api/", StringComparison.OrdinalIgnoreCase))
                {
                    response = new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK,
                        Content = new StringContent("hello world!")
                    };
                }
    
                return response;
            }
    
        }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 21, 2016 6:04 PM

All replies

  • User36583972 posted

    Hi Jamie Ide,

    As far as I know, If you use OWIN, it could be possible to use Web API DelegatingHandler in MVC but this is still in beta. So, It maybe has some problems.

    I suggest you can refer the following links and use the stable technology.

    1: HTTP Message Handlers in ASP.NET Web API:

    http://www.asp.net/web-api/overview/advanced/http-message-handlers

    2: Why does a call to an ASP.NET MVC Controller not execute a DelegatingHandler:

    http://stackoverflow.com/questions/23507283/why-does-a-call-to-an-asp-net-mvc-controller-not-execute-a-delegatinghandler

    Best Regards,

    Yohann Lu

    Monday, April 18, 2016 7:09 AM
  • User272637413 posted

    Thanks for your reply. I don't think I'm using anything that is in beta and a DelgatingHandler is a message handler.

    I tried several combinations of startup code before I found a workaround. In the handler, I am checking if the route starts with "/api/" and only wrapping the response if it does.

        public class WtfDelegatingHandler : DelegatingHandler
        {
            protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                var response = await base.SendAsync(request, cancellationToken);
    
                if (request.RequestUri.LocalPath.StartsWith("/api/", StringComparison.OrdinalIgnoreCase))
                {
                    response = new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK,
                        Content = new StringContent("hello world!")
                    };
                }
    
                return response;
            }
    
        }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 21, 2016 6:04 PM