【问题标题】:ASP.NET Web-API OWIN: No HTTP resource was found that matches the request URIASP.NET Web-API OWIN:未找到与请求 URI 匹配的 HTTP 资源
【发布时间】:2015-08-20 03:16:40
【问题描述】:

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

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

当我尝试访问 `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>

`Server类是主类:

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();
            }

        }
    }
}

这是我的 Web API 配置类:

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 类只是将应用程序添加到控制器使用的应用程序列表中。

【问题讨论】:

    标签: c# asp.net rest asp.net-web-api owin


    【解决方案1】:

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

    【讨论】:

    • 解决了这个问题。我以为我已经尝试过了,但我一定是尝试了错误的端口号或有错字。显然我也不能在我的 Application 类中使用构造函数,因为它会引发 InvalidDataContractException 错误。
    【解决方案2】:

    我也遇到过这个问题。经过一番挣扎后,我意识到我的 Controller 类是私有的,这导致了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      相关资源
      最近更新 更多