ASP.NET WebAPI Selfhost未解析到控制器的路由

时间:2021-03-05 18:07:28

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.

我正在使用版本“5.0.0-rc1”中的“Microsoft.AspNet.WebApi.SelfHost”NuGet包(也试过当前的稳定版)但遗憾的是我无法通过我的应用程序设置来解析配置的路由到我的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.

该应用程序非常简单,几乎可以在所有示例中找到。它包含一个.NET控制台应用程序,其中主要类是自托管服务。

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.

然后有一个单独的控制器类,它位于同一个项目中,并继承自ApiController类。

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!

谢谢你到目前为止!

1 个解决方案

#1


14  

Web API controllers need to be public.

Web API控制器需要公开。

#1


14  

Web API controllers need to be public.

Web API控制器需要公开。