【发布时间】: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