我正在使用Web API 2并在ASP.Net 4中开发。这是我试图学习webapi的示例代码。有两条路线。第一条路线是给定商店的服务资源。第二条路线是商店资源路线。为什么Web API没有找到我的资源?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Services",
url: "store/{id}/services",
defaults: new { controller = "Services" }
);
routes.MapRoute(
name: "Store",
url: "store/{id}",
defaults: new { controller = "Store", id = UrlParameter.Optional}
);
}
第二条路线“商店”完美地工作。第一条路线是详细介绍商店中可用的所有服务。当我尝试
/API /存储/ 1 /服务
我得到404错误。有人能指点我做错了什么吗?
这里是控制器
namespace APITestter1.Controllers
{
public class ServicesController : ApiController
{
public string Get(int id, string prop = "xxx")
{
return "Hello services World!" + id + " added attribute " + prop;
}
public string Post(int id, string prop = "xxx")
{
return "Hello Post World!" + id + " added attribute " + prop;
}
}
}