[英]ASP.NET Web API: Optional Guid parameters
我有ApiController和Get动作这样:
public IEnumerable<Note> Get(Guid userId, Guid tagId)
{
var userNotes = _repository.Get(x => x.UserId == userId);
var tagedNotes = _repository.Get(x => x.TagId == tagId);
return userNotes.Union(tagedNotes).Distinct();
}
我希望以下请求针对此操作:
我该怎么做?
更新 :小心,api控制器不应该有没有参数的另一个GET方法,或者你应该使用一个可选参数的动作。
您需要使用Nullable类型(IIRC,它可能使用默认值( Guid.Empty
)
public IEnumerable<Note> Get(Guid? userId = null, Guid? tagId = null)
{
var userNotes = userId.HasValue ? _repository.Get(x => x.UserId == userId.Value) : new List<Note>();
var tagNotes = tagId.HasValue ? _repository.Get(x => x.TagId == tagId.Value) : new List<Note>();
return userNotes.Union(tagNotes).Distinct();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.