伟大的,
我的 asp.net mvc4 研究项目有问题。我尝试创建具有多个 get 操作的 apicontroller,其中一些带有参数,而另一些则没有。
我的 API
public class ContentController : ApiController
{
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories1()
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories2()
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories3(int i)
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
[System.Web.Http.HttpGet]
public HttpResponseMessage Categories4(int i)
{
return Request.CreateResponse(HttpStatusCode.OK, new string[] { "Toys", "Games", "Animals", "Woob-Woob" }, Configuration.Formatters.JsonFormatter);
}
}
我的 apiConfig:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ApiByActionWithId",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}"
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
以及这个结果
对于 /api/content/categories1,结果是 ["Toys","Games","Animals","Woob-Woob"]
对于 /api/content/categories3 和 /api/content/categories3/1 结果 - “未找到与请求 URI 'http://localhost:51200/api/content/categories3' 匹配的 HTTP 资源。”
我不知道应该如何处理