ChrisFletcher
ChrisFletcher

Reputation: 1042

WebApi in OWIN always returning 200 instead of 404 when not matching a route

I have a relatively simple WebApi site with a couple of controllers, running in OWIN with AutoFac as the DI container.

The site was setup using attribute routes only but always returns a 200 OK response even if we hit an invalid route. We had some filters and a static file server running also but I've commented out all of the code within our startup file (all IAppBuilder calls and even the creation of the HttpConfiguration) but we still get this behaviour. This happens in both IIS and IIS Express.

I've also tried adding a default route in but have seen the same behaviour.

I've done some reading and I understand that I can write some kind of hook into the pipeline or write a controller with a catch all route and an action that returns a 404, but it feels as though that shouldn't be necessary.

Is this intended to be the default behaviour?

I've looked at this answer but we don't have a global.asax: ASP.NET Web Api returns 200 OK when it should return 404

See reduced code below that still demonstrates the issue

Startup.cs

[assembly: OwinStartup(typeof(Startup))]
namespace Api.blah
{
    using Owin;

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();

            var container = this.GetAutofacContainer(config);
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            config.MapHttpAttributeRoutes();

            app.UseWebApi(config);
        }

        private IContainer GetAutofacContainer(HttpConfiguration config)
        {
            ContainerBuilder containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            return containerBuilder.Build();
        }
    }

}

HealthController.cs

namespace Api.blah.Controller
{
    [RoutePrefix("api/health")]    
    public class HealthController : ApiController
    {
        public HealthController()
        {

        }

        [HttpGet]
        [Route]
        public HealthResponse Get()
        {
            return new HealthResponse { Alive = true, Healthy = true };
        }
    }
}

If I access anything other than the api/health route (eg http://localhost:1333/zz) then I get a 200. The original code is larger than this, but I've reduced it significantly as I explained above and the same behaviour persists

Upvotes: 6

Views: 846

Answers (1)

Jason Rowe
Jason Rowe

Reputation: 6286

This issue can happen if your web.config contains the StaticFileModule

<handlers>
    <!-- Remove all handlers -->
    <clear />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="Unspecified" requireAccess="Read" />
</handlers>

If you need static files, you can work around this by using the path:

<add name="StaticFile" path="/staticfiles" verb="*" modules="StaticFileModule" resourceType="Unspecified" requireAccess="Read" />

Upvotes: 0

Related Questions