如何使用 Web 触发调试 Api
How to trigger debugging with Web Api
是否可以导航到存在网络 api 的 URL?我的努力表明不,常识说它应该是。因此,我想我还有其他地方的错误。
我已经启动 VS 2015,添加了一个新项目 (ASP.NET) 并选择了 web api。我已经创建了我的 api
public class DefaultController : ApiController
{
public void Index()
{
//logic with break points set
}
}
我还创建了一个 'normal' MVC 控制器,因此,当我启动项目时,我的 Web 浏览器会在我的 Home 控制器中显示索引文件。
现在,我想导航到我的网站 api 的关联 URL
我的家庭控制器(索引视图)的 URL 是
http://localhost:61895/
因此我的 api 应该是(我认为)
http://localhost:61895/api/default/
但我在浏览器中看到以下内容
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
我的 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
Web Api 在路由上的工作方式与 Mvc 控制器不同。
网络中没有动作部分api。
http 操作必须与操作或属性的开头相匹配。例如:
public class DefaultController : ApiController
{
public void GetIndex()
{
//logic with break points set
}
}
或
public class DefaultController : ApiController
{
[HttpGet]
public void Index()
{
//logic with break points set
}
}
url 将是
localhost:61895/api/Default
asp.net有一个很好的例子:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
编辑
添加一些屏幕截图。
我创建了一个新的 web api 项目,并创建了新的控制器 default:
public class DefaultController : ApiController
{
public void GetIndex()
{
var a = "a";
}
}
然后我的url:
http://localhost:4349/api/default
如您所见,断点被击中。
根据评论中的讨论,问题是 Global.asax.cs 文件
顺序很重要,必须按以下顺序
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
GlobalConfiguration.Configure(WebApiConfig.Register);
必须在
之前
RouteConfig.RegisterRoutes(RouteTable.Routes);
是否可以导航到存在网络 api 的 URL?我的努力表明不,常识说它应该是。因此,我想我还有其他地方的错误。
我已经启动 VS 2015,添加了一个新项目 (ASP.NET) 并选择了 web api。我已经创建了我的 api
public class DefaultController : ApiController
{
public void Index()
{
//logic with break points set
}
}
我还创建了一个 'normal' MVC 控制器,因此,当我启动项目时,我的 Web 浏览器会在我的 Home 控制器中显示索引文件。
现在,我想导航到我的网站 api 的关联 URL 我的家庭控制器(索引视图)的 URL 是
http://localhost:61895/
因此我的 api 应该是(我认为)
http://localhost:61895/api/default/
但我在浏览器中看到以下内容
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
我的 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
中调试我的 APIWeb Api 在路由上的工作方式与 Mvc 控制器不同。
网络中没有动作部分api。
http 操作必须与操作或属性的开头相匹配。例如:
public class DefaultController : ApiController
{
public void GetIndex()
{
//logic with break points set
}
}
或
public class DefaultController : ApiController
{
[HttpGet]
public void Index()
{
//logic with break points set
}
}
url 将是
localhost:61895/api/Default
asp.net有一个很好的例子:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
编辑
添加一些屏幕截图。 我创建了一个新的 web api 项目,并创建了新的控制器 default:
public class DefaultController : ApiController
{
public void GetIndex()
{
var a = "a";
}
}
然后我的url:
http://localhost:4349/api/default
如您所见,断点被击中。
根据评论中的讨论,问题是 Global.asax.cs 文件
顺序很重要,必须按以下顺序
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
GlobalConfiguration.Configure(WebApiConfig.Register);
必须在
之前RouteConfig.RegisterRoutes(RouteTable.Routes);