Unable to get ASP.NET MVC and Web API routing straight in my mind - getting 404s galore

694 views Asked by At

I am struggling with routing issues in my ASP.NET MVC + WebAPI app. I feel like I have a moving target here because when I add or change one route I break another. Here is my specific example.

First, my controllers, so you can see the names:

enter image description here

Next, my MVC routing config (I understand that my config is probably duplicative, but it is because I am trying things):

enter image description here

And my Web API routing config (I understand that my config is probably very duplicative, but it is because I am trying things):

enter image description here

As a routing problem example, here's the PodcastFeeds API controller:

enter image description here

So I post to the Create action method...

enter image description here

And as you can see I get the error: 404 not found - "No HTTP resource was found that matches the request URI.

I would love some direction here...

1

There are 1 answers

6
Alex Art. On BEST ANSWER

The order or registering routes is important since requests are handled by the firs route that matches the patter. Because your default route is the first one it always selected. You need to put your default route to be the last one and register all your routes starting from the most constraining and ending with default one. Also Is I see in your screenshots some of your routes are not fully configured: for example api/PodcastFeeds route doesn't specify a controller (it needs to look similar to your FeedRouteMap route from the second screenshot):

routes.MapHttpRoute(
  name: "PodcastFeeds",
  url: "api/PodcastFeeds/{action}/{id}",
  defaults: new {controller="PodcastFeeds", action="Create", id=UrlParameter.Optional})

As an alternative you can use attribute routing to avoid those kind of issues.