我的WebApiConfig
中有以下路线 config.Routes.MapHttpRoute(
name: "PaginateMessages",
routeTemplate: "api/Message/PaginateMessages/{conversationId}/{lastMessageId}",
defaults: new { controller = "Message", action = "PaginateMessages", conversationId = RouteParameter.Optional, lastMessageId = RouteParameter.Optional }
);
我在Message控制器中有相应的操作:
[HttpGet]
public async Task<List<MessageDTO>> PaginateMessages(int conversationId, int lastMessageId)
{
return null;
}
然而,当我尝试点击端点时,我得到了404:
http://localhost:60162/api/Message/PaginateMessages/71/150
如上所示,是否不可能有多个路由参数?
答案 0 :(得分:1)
您需要在路线模板中添加{controller}
和{action}
。
config.Routes.MapHttpRoute(
name: "PaginateMessages",
routeTemplate: "api/{controller}/{action}/{conversationId}/{lastMessageId}",
defaults: new {controller = "Conversation", action = "GetConversation", conversationId = RouteParameter.Optional, lastMessageId = RouteParameter.Optional}
);
Web API 2中的替代方法是在WebApiConfig中删除自定义路由配置,并使用Route属性。
public class MessageController : ApiController
{
[HttpGet]
[Route("api/Message/PaginateMessages/{conversationId}/{lastMessageId}")]
public async Task<List<MessageDTO>> PaginateMessages(
int conversationId, int lastMessageId)
{
return null;
}
}