ASP.NET Web-API OWIN:找不到与请求 URI 匹配的 HTTP 资源

ASP.NET Web-API OWIN: No HTTP resource was found that matches the request URI

感谢本网站一些用户的帮助,我的测试应用程序开始运行,现在开始编写我需要的实际应用程序。

我有一个 C# 应用程序,它使用 OWIN 来托管 ASP.NET Web API。我需要该应用程序提供有关其他打开的应用程序的信息。我编写的程序几乎与我开始工作的 test/example 程序完全相同,但更改了一些名称。

当我尝试访问 `http://localhost:9000/api/applications 时,我收到一条错误消息:

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

`服务器class为主class:

namespace Server
{
    public class Server
    {
        public const string baseAddress = "http://localhost:9000/";
        static void Main(string[] args)
        {
            ApplicationManager.getManager().AddApplication(new Application(1, "Chrome", 0.5f));
            ApplicationManager.getManager().AddApplication(new Application(2, "Windows Media Player", 1.0f));
            ApplicationManager.getManager().AddApplication(new Application(3, "Minecraft", 1.0f));

            using (WebApp.Start<Startup>(url: baseAddress))
            {
                Console.ReadKey();
            }

        }
    }
}

这是我的网站 API 配置 class:

namespace Server
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            appBuilder.UseWebApi(config);
        }
    }
}

这是我的控制器:

namespace Server.Api.Controllers {
    public class ApplicationController : ApiController
    {
        public IEnumerable<Application> Get()
        {
            return ApplicationManager.getManager().GetApplications();
        }

        public IHttpActionResult Get(int id)
        {
            Application app = ApplicationManager.getManager().GetApplication(id);
            if (app == null)
            {
                return NotFound();
            }

            return Ok(app);
        }
    } }

Application 对象只包含一些属性和构造函数。 ApplicationManager class 只是将应用程序添加到控制器使用的应用程序列表中。

http://localhost:9000/api/applications 替换为 http://localhost:9000/api/application。最后一个字母 s 导致了这个问题。希望这对您有所帮助!

我也遇到了这个问题。经过一番努力后,我意识到我的控制器 class 是私有的,这是导致问题的原因。