Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET WebAPI Selfhost didn't resolve route to controller

I'm playing with the "Microsoft.AspNet.WebApi.SelfHost" NuGet package in version "5.0.0-rc1" (tried also the current stable) but unfortunately I can't get my application setup to resolve the configured route to my implementation of the ApiController.

The application is quite simple and can be found nearly in all examples. It contains a .NET console application where in the main class the self host service lives.

using System.Web.Http.SelfHost;
using System.Web.Http;
using System;
namespace EP.Server
{
class Program
{
    static void Main(string[] args)
    {
        var config = new HttpSelfHostConfiguration("http://localhost:60064");
        config.Routes.MapHttpRoute(
            name: "Default Route",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        using (var server = new HttpSelfHostServer(config))
        {
            server.OpenAsync().Wait();
            Console.WriteLine("Server ist running on "+config.BaseAddress + " hit ENTER to stop.");
            Console.ReadLine();
            server.CloseAsync().Wait();
        }
    }
}
}

and then there is a separate controller class which lives in the same project and inherits from the ApiController class.

using System.Web.Http;

namespace EP.Server
{
class UsersController : ApiController 
{
    [HttpGet]
    public string Get()
    {
        return "Hello I'm a user...";
    }
}
}

The error message sounds like this when I call the service from any browser.

<Error>
<Message>No HTTP resource was found that matches the request URI   'http://localhost:60064/api/Users'.</Message>
<MessageDetail>No type was found that matches the controller named 'Users'.</MessageDetail>
</Error>

Does somebody have an idea what's wrong there? Unfortunately I couldn't find any similar issues / questions on the web.

Thank you so far!

like image 979
ddankovics Avatar asked Sep 23 '13 06:09

ddankovics


1 Answers

Web API controllers need to be public.

like image 150
Filip W Avatar answered Oct 03 '22 00:10

Filip W