Mike Rouse
Mike Rouse

Reputation: 1298

Routing to a hard-coded Controller

This route mapping doesn't work:

configuration.Routes.MapHttpRoute(
    "EnvironmentTargetsView",
    "api/EnvironmentTargetsView/{id}/{userGuid}",
    new
    {
     id = RouteParameter.Optional,
     userGuid = RouteParameter.Optional,
    });

I get the error: "No route providing a controller name was found to match request URI"

However, this route mapping does work:

configuration.Routes.MapHttpRoute(
    "EnvironmentTargetsView", "api/{Controller}/{id}/{userGuid}",
    new
    {
     Controller = "EnvironmentTargetsView",
     id = RouteParameter.Optional,
     userGuid = RouteParameter.Optional,
    });

I am curious about why and have surfed for answers on here, but can't really figure it out. I want to hard-code that value because it is a specific route I want the API to take. My worry is by having it tokenised in the routeTemplate I can now not use a route with a similar pattern.

Upvotes: 1

Views: 1079

Answers (1)

Elger Mensonides
Elger Mensonides

Reputation: 7029

It's because you specified:

Controller = "EnvironmentTargetsView"

in the second code block. If you add that to the first code block it'll work and still have a hard coded value.

You could also just add something like:

[Route("/api/EnvironmentTargetsView/{id}/{userGuid}]
public void Get(int id, guid userGuid) { }

To your controller methods.

Upvotes: 2

Related Questions