1

I am trying to write a self hosted WebAPI server. I want all routes to go to a single controller. This controller can pick out the controller part of the url and use this to decide an appropriate response.

I have the following route configuration:

_configuration.Routes.MapHttpRoute
(
    name: "DefaultApi",
    routeTemplate: string.Concat("api/Home", "/{id}"),
    defaults: new { id = RouteParameter.Optional, controllerName="Home" }
);

My controller class is called "HomeController". I'm trying to redirect all URLs to it.

Here is the code for HomeController. For now I have commented out the calls to external logic (remote controller). It should just be returning a string on the Get action.

public class HomeController : ApiController
{
    private IExternalController<string> remoteController;

    public HomeController()
    {
        remoteController = GlobalKernelConfiguration.GetStandardKernel().Get<IExternalController<string>>();
    }

    public string Get()
    {
        return "HELLO FROM INTERNAL"; //remoteController.Get();
    }

    public string Get(int id)
    {
        return remoteController.Get(id);
    }

    public void Delete(int id)
    {
         remoteController.Delete(id);
    }

    public void Post(string value)
    {
        remoteController.Post(value);
    }

    public void Put(int id, string value)
    {
        remoteController.Put(id, value);
    }
}

I would expect http://localhost:9000/api/[AnythingHere] to route to the home controller but I get the following error when trying the following url: http://localhost:9000/api/Home

{"Message":"No HTTP resource was found that matches the request URI 'http://loca lhost:9000/api/Home'.","MessageDetail":"No route providing a controller name was found to match request URI 'http://localhost:9000/api/Home'"}

K-Dawg
  • 3,013
  • 2
  • 34
  • 52
  • So, what does or doesn't this do? – CodeCaster Mar 23 '16 at 11:30
  • @CodeCaster I went to print the header information from my console application and I discovered an error. I have attached it to the question. – K-Dawg Mar 23 '16 at 11:43
  • Is your `HomeController` an `ApiController`? – CodeCaster Mar 23 '16 at 11:45
  • @CodeCaster - Yes it is. I have added the code to the home controller to the question. – K-Dawg Mar 23 '16 at 11:45
  • Why do you use `controllerName` and not `controller` in your defaults? – CodeCaster Mar 23 '16 at 11:46
  • @CodeCaster - I couldn't see controller in the intelisense but controllerName popped up. I changed it to controller and it worked! Thank you! – K-Dawg Mar 23 '16 at 11:50
  • `I would expect...` Why would you expect that? That's not the purpose of `defaults` in a route. – Kenneth K. Mar 23 '16 at 11:51
  • @KennethK. I am doing something a little different. I am creating a a component that accepts classes that conform to an interface as external controllers. I need to have a single controller that would call these external classes and perform their actions. I am merely hypothesising what would happen if I were to attempt to use the technology an an unconventional way. – K-Dawg Mar 23 '16 at 11:55

1 Answers1

2

As @CodeCaster suggested in the comments the problem was caused by not using the correct parameter in the routing options.

This is what I had before

_configuration.Routes.MapHttpRoute ( name: "DefaultApi", routeTemplate: string.Concat("api/Home", "/{id}"), defaults: new { id = RouteParameter.Optional, controllerName="Home" } );

this is what I have now:

        public static void AddControllerRoute(string controllerName)
    {
        _configuration.Routes.MapHttpRoute
           (
               name: "DefaultApi",
               routeTemplate: string.Concat("api/Home", "/{id}"),
               defaults: new { id = RouteParameter.Optional, controller ="Home" }
           );
    }

notice that the defaults parameter was changed and now uses "controller" instead of "controllerName" this solved the problem and it's now working.

K-Dawg
  • 3,013
  • 2
  • 34
  • 52