【发布时间】:2016-09-28 01:23:17
【问题描述】:
我正在开发一个 ASP.NET MVC Web Api。我正在为我的 api 重写路由以使 url 整洁。但它不起作用。
我在 ItemsController 中有这样的操作方法:
public HttpResponseMessage Get([FromUri]string keyword = "", [FromUri]int category = 0, [FromUri]int region = 0, [FromUri]int area = 0, [FromUri]int page = 0,[FromUri]int count = 0)
{
.
.
.
}
在 WebApiConfig 中,我为该操作配置路由,如下所示:
config.Routes.MapHttpRoute(
name: "",
routeTemplate: "api/v1/places/{category}/{region}/{area}/{page}/{count}",
defaults: new { controller = "ItemsController" , keyword = "" , category = 0 , region = 0 ,area = 0 , page = 0 , count = 0 }
);
如您所见,我没有在路由中设置关键字。但是当我从下面的 url 访问时,它给了我错误。
这就是我提出获取请求的方式:
http://localhost:50489/api/v1/places/0/0/0/1/2
这是错误:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:50489/api/v1/places/0/0/0/1/2'.",
"MessageDetail": "No type was found that matches the controller named 'ItemsController'."
}
我该如何重写它?我想在该网址中排除关键字。我也会有另一条路线来执行该操作。
这也不好用:
config.Routes.MapHttpRoute(
name: "",
routeTemplate: "api/v1/places/{keyword}/{category}/{region}/{area}/{page}/{count}",
defaults: new { controller = "ItemsController" , keyword = "" , category = 0 , region = 0 ,area = 0 , page = 0 , count = 0 }
);
【问题讨论】:
标签: asp.net-mvc asp.net-web-api routes url-routing asp.net-web-api-routing