【发布时间】:2015-09-05 12:34:09
【问题描述】:
是否可以导航到存在 Web api 的 URL?我的努力表明不,常识说它应该是。因此,我想我在其他地方有错误。
我已经启动了 VS 2015,添加了一个新项目 (ASP.NET) 并选择了 web api。我已经创建了我的 api
public class DefaultController : ApiController
{
public void Index()
{
//logic with break points set
}
}
我还创建了一个“普通”MVC 控制器,因此,当我启动项目时,我的网络浏览器会显示我的 Home 控制器中的索引文件。
现在,我想导航到我的 web api 的关联 URL 我的主控制器(索引视图)的 URL 是
http://localhost:61895/
因此我的 api 应该是(我认为)
http://localhost:61895/api/default/
但我在浏览器中看到以下内容
HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。
而我的 WebApiConfig 是
public static class 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 }
);
}
}
编辑
我也试过localhost:61895/api/Default/Index,但同样的问题
如何在 Visual Studio 中调试我的 API
【问题讨论】:
-
我想您可能正在寻找 localhost:61895/api/Default/Index 作为 URL。
-
很遗憾不是@MartinNoreke :(
标签: asp.net-mvc asp.net-web-api2