【发布时间】:2014-09-15 05:48:12
【问题描述】:
我正在学习 webapi。我正在运行我的 webapi 项目并点击 url localhost:54099/api/values
但是 Get 没有执行。我看不到数据。我在哪里做错了?当我点击 url 时,下面的弹出窗口显示,但我既不能保存也不能打开它。 当我在 Chrome 中点击相同的 url 时,我收到了错误
尝试创建“ValuesController”类型的控制器时出错。确保控制器有一个无参数的公共构造函数。
ValuesController 中的WebAPI 方法
public IEnumerable<Book> Get()
{
var Books = iLibrary.GetBooks();
return Books.ToList();
}
WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
【问题讨论】:
-
这个问题发生在ie only..test it on chrome or other browser
-
如果您想使用外部客户端应用程序来测试您的 API,您应该熟悉 Fiddler 之类的东西。还可以考虑编写单元和集成测试,这样您就可以断言您的期望,而不是使用外部应用手动确认它们。
标签: asp.net asp.net-mvc rest asp.net-web-api