设置路由,以便在WebApi中使用不同的“id”获取实体
本文关键字:id 实体 获取 路由 WebApi 设置 | 更新日期: 2023-09-27 18:15:23
假设我有以下模型:
public class Comment
{
public long Id { get; set; }
public long UserId { get; set; }
public long AccountId { get; set; }
public long EventId { get; set; }
public string Body { get; set; }
public System.DateTime TimeStamp { get; set; }
}
我已经设置了它,以便GET /comments/
返回您帐户的所有评论GET /comments/123
返回一条注释。通过默认路由,这不是问题。
但是我想知道如何能够有两个额外的GET端点,一个能够返回EventId
的所有评论,另一个能够返回UserId
的所有评论。
任何想法都会很感激!
你可以修改你的路由来查找方法名(动作):
{controller}/{action}/{id}
,那么你可以有多个GET
方法,将清楚地说明他们的意图:
GetCommentsForUser();
GetCommentsForEvent();
GetCommentsForAccount();
当然URL是
/comments/GetCommentsForUser/123