I have an ASP.net MVC 3 site with these types of routes:
routes.MapRoute("Get", "endpoint/{id}", new { Controller = "Foo", action = "GetFoo" }, new { httpMethod = new HttpMethodConstraint("GET") }); routes.MapRoute("Post", "endpoint/{id}", new { Controller = "Foo", action = "NewFoo" }, new { httpMethod = new HttpMethodConstraint("POST") }); routes.MapRoute("BadFoo", "endpoint/{id}", new { Controller = "Error", action = "MethodNotAllowed" }); routes.MapRoute("NotFound", "", new { controller = "Error", action = "NotFound" });
So, in a nutshell, I have a Route that matches certain HTTP verbs like GET and POST, but on other HTTP verbs like PUT and DELETE, it should return a specific error.
My default route is 404.
If I delete the βBadFooβ route, then PUT against the endpoint / {id} returns 404, because none of the other routes matches, so it goes to my NotFound route.
The fact is that I have a ton of routes, such as Get and Post, where I have HttpMethodConstraint, and where I will need to create a route, for example a BadFoo route, to catch the correct match in the route string, but not on the method that is redundant way disables my routing.
How to configure routing only with Get, Post, and NotFound routes, but distinguish between HTTP 404 not found (= invalid URL) and HTTP method 405 not allowed (= valid URL, invalid HTTP method)?