0

This might be a weird question, but I struggled for hours with this issue and at the end I have no idea what is happening and feel like I fundamentally misunderstood something about all of this.

Following example:

[RoutePrefix("api/customers")]
public class ExampleController : Controller
{
    [HttpGet]
    [Route("{customerId}")]
    public ActionResult Read(string customerId)
    {
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }

    [HttpPost]
    [Route("")]
    public ActionResult Create(CreateCustomerModel model)
    {
        // do stuff
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }
}

When I design my controller like this, using an MVC controller, I get an error when I try to make a request to the first action (GET localhost:1234/api/customers/blabla)

I did a lot of trial and error to find out why this is happening. I get the following error message btw (which is in german unfortunately): enter image description here

Translated:

Message: No HTTP-Resource was found, that matches the Request-URI \"http://localhost:53741/api/customers/blabla\"

MessageDetail: No Type was found, that matches a controller with the name \"customers\"

Now, the thing is, if I change the Routeprefix, so it doesn't contain "api", but something else instead, for example "mapi" (or whatever), then it works! So I was wondering if "api" is some kind of keyword. I then changed the Controller to ApiController (and adjusting all the types and stuff), and then it still worked!

This makes it seem like when the route starts with the word "api", the programm expects an ApiController? Is that true? Why?

You might say, why don't you just use an ApiController, it's made exactly for the purpose of creating an API, and you are probably right in some way, but I only had bad experiences with the ApiController compared to the MVC controller. It is much more restricted and less advanced (for example you can only bind a single parameter from the body, I forgot about all the other restrictions). I researched many times why you should use ApiController and when you should use Controller, but never found a compelling argument for ApiController, because I can do everything with Controller too (my only guess is that it has some overhead or something). Anyways, that is not the point of the question, the point is why can't I use "api" in Controller.

This is ASP.NET Framework 4.7.2 btw.

crowbar
  • 39
  • 3
  • Maybe you could translate that error message to english for everyone. – itsme86 Jun 15 '20 at 15:24
  • It's something like "No HTTP resource was found that matches the request URI in Web API" (which I found when googling the german error), but this english error message contains "Web API", which mine doesn't. – crowbar Jun 15 '20 at 15:25
  • MessageDetail says "No Type was found, that matches the controller with the name \"customers\" – crowbar Jun 15 '20 at 15:26
  • Btw when I make a request to a route that doesn't exist, then I get a completely different error (which is much longer and more detailed). I never saw this error before. – crowbar Jun 15 '20 at 15:30

0 Answers0