We would like to write a piece of OWIN middleware that looks at the current request and see if it matches any mapped routes (we are using web.api, so we register routes with the httpRouteCollection.MapHttpRoute
method.
If the current request does not match any known configured routes we would like to reject it with a custom 404 message.
Is it possible to determine from the OWIN context whether the current request matches one of the defined routes?
The short answer is, no, you cannot. OWIN middleware does not designed to do that. It is independent of the application.
If you want a custom 404 error page, you can add a setting in the web.config file to do this for you:
<configuration>
<system.webServer>
<httpErrors>
<error statusCode="404" path="/somedir/oops404.htm" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>
Hope that helps.