0

I am trying to refactor an old setup, the end goal will be that I will have a functional REST API. I am getting rid of an old NuGet service and I sort of have to rebuild everything now. I am trying to set it up using ASP.NET Web API. This will include 60+ routes (like "www.website.com/cars/{id}/engine" "www.website.com/inventory/{id}" etc..)

I have tried attribute routing and conventional routing, and nothing seems to work. I get 404's no matter what I seem to try. I am probably just not doing either of them correctly.

Here is how I am setting up configuration (I am using a self hosting package btw but this is the main configuration point):

public class SomeHostClass
{

public void Configuration(IAppBuilder app)
 { 
  HttpConfiguration config = new HttpConfiguration();

  //If I am trying attribute based routing, uncomment this
  //config.MapHttpAttributeRoutes();

  Register(config.Routes);

  // I think I need this at all times?
  app.UseWebApi(config);
 }

public void Register(HttpRouteCollection routes)
 {
    // One of many other routes.MapHttpRoute() calls
    routes.MapHttpRoute(
    name: "Cars",
    routeTemplate: "cars/{id}/engine",
    defaults: new
    {
      controller = "CarController",
      action = "Get"
    },
    constraints: null);
 }

}

Then here is my Car Controller:

public class CarController : ApiController
{
    [HttpGet]
    public object Get([FromUri] CarDTO carObject)
    {
        // Some code calling a private worker method
        return WorkerMethod();
    }

    private object WorkerMethod()
    {
        // Worker method do stuff, return
    }
}

This Get() method is never called and a 404 is returned.

Another note: I have tried using reflection to register all the routes, which slims down the code. I'll debug it and it looks like all the routes have been configured correctly within the HttpConfiguration.routes but I will get 404's still. Even without reflection doing it the way shown above- if I debug and look at the values they all seem correct, but don't work.

Another Note: I should also mention that this is the error I usually get along with the 404.

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:8000/cars/1/engine'.","MessageDetail":"No type was found that matches the controller named 'CarController'."}

How do I get these routes recognized and stop returning 404's and start returning my data?

If this is a bad way of going about this- what's the best way?

15
  • Are you allowed to update the routes or is it required to use the same routes?
    – ShawnOrr
    Commented Jul 21, 2021 at 1:09
  • You have only one route configured? Is the default route configuration added first or last? What's the API URL you are trying to call?
    – Chetan
    Commented Jul 21, 2021 at 1:33
  • I recall a thing with similar problems to yours where we were upgrading old code, and that turned out to be we'd imported the wrong namespace for the HttpGet etc attributes - they're present in two NS and need to be matched for the type of controller used. Can't quite remember the exact details, but it was something like "if it's an API controller it needs routing attributes from X namespace, if it's an MVC controller it needs routing attributes from Y namespace" think one of the namespaces mentioned mvc and the other didn't.. I'll see if I can find some more detail
    – Caius Jard
    Commented Jul 21, 2021 at 5:07
  • You could also look at making a new project of the type of service you want to make and check that the weather forecast controller (or whatever the sample is) does work as you expect, then copy the namespaces, and base class part of the file and paste into what you're upgrading/ or compare them to see if anything is different
    – Caius Jard
    Commented Jul 21, 2021 at 5:08
  • 1
    Incidentally, I'm not sure what you will expect to happen with your setup as it is- you've got a route that has an {id}, and you've got a method that says an entire dto is encoded in the uri- are you expecting some background magic to turn eg an ID Integer into a DTO? If a complex object is an api controller method parameter I would expect that it is more often FromBody and is a block of Json as the request payload, not encoded in the uri. Perhaps start from a working sample and proceed with modifying it til it doesn't.
    – Caius Jard
    Commented Jul 21, 2021 at 5:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.