Web API 方法 Returns 结果集,即使参数不存在

Web API Method Returns Result Set Even If Parameter Doesn't Exist

我一直在研究路由和可选参数,但我遇到了一个问题,无论我从数据库中得到什么结果。这是我在控制器中的方法:

[Route("{companyID:int}/contact/{uid?}")]
[HttpGet]
public IQueryable GetCompanyContactsByID(int companyID, Guid? uid = null)
{
    IQueryable<vwCompanyContact> contact = coreDB.vwCompanyContacts.Where(con => con.IDCompany == companyID);

        if (uid != null)
            contact = contact.Where(con => con.CatalogNumber == uid);

    return contact;
}

所以基本上如果调用 (http://localhost:21598/DAL/api/company/100/contact/) I get a list of all the contacts of company 100. And then if I call (http://localhost:21598/DAL/api/company/100/contact/64077706-b7c9-e411-825d-28b2bd14ba94666) I get just one record with the matching the GUID. But then if I call (http://localhost:21598/DAL/api/company/100/contact/marryhadalittlelamb) 我会再次获得所有联系人的列表。

我宁愿收到一个空的结果集,或者收到一条说没有找到结果的回复消息。我不确定如何解决这个问题,因为我对 C# 和 Linq 还是很陌生。

这是我得到的最终代码:

[Route("{companyID:int}/contact/{uid?}")]
[HttpGet]
public IHttpActionResult GetCompanyContactByID(int compantID, string uid = null)
{
    IQueryable<vwCompanyContact> contact 
        = coreDB.vwCompanyContacts.Where(con => con.IDCompany == companyID);

    if (!string.IsNullOrEmpty(uid))
    {
        Guid uidValue;
        if (Guid.TryParse(uid, out uidValue))
            contact = contact.Where(con => con.CatalogNumber == uidValue);
        else
            return StatusCode(HttpStatusCode.NoContent);;
    }

    return Ok(contact);
}

您可以将参数声明为 string,如果无法解析为 Guid:

则拒绝调用
[Route("{companyID:int}/contact/{uid?}")]
[HttpGet]
public IQueryable GetCompanyContactsByID(int companyID, string uid = null)
{
    Guid? uidValue = null;
    if(!string.IsNullOrEmpty(uid) && !Guid.TryParse(uid, out uidValue))
         throw new ArgumentException("uid");

    IQueryable<vwCompanyContact> contact
        = coreDB.vwCompanyContacts.Where(con => con.IDCompany == companyID);

        if (uidValue.HasValue)
            contact = contact.Where(con => con.CatalogNumber == uidValue.Value);

    return contact;
}

或者您可以有两个单独的重载:

[Route("{companyID:int}/contact/")]
[HttpGet]
public IQueryable GetCompanyContactsByID(int companyID)
{
    IQueryable<vwCompanyContact> contact
        = coreDB.vwCompanyContacts.Where(con => con.IDCompany == companyID);
    return contact;
}

[Route("{companyID:int}/contact/{uid}")]
[HttpGet]
public IQueryable GetCompanyContactsByID(int companyID, Guid uid)
{
    IQueryable<vwCompanyContact> contact
        = coreDB.vwCompanyContacts.Where(con => con.IDCompany == companyID
                                                && con.CatalogNumber == uid);

    return contact;
}
public class MyResponse
{
   public IList<vwCompanyContct> myContacts {set;get;}
   public string ResponseMessage {set;get;}
}

[Route("{companyID:int}/contact/{uid?}")]
[HttpGet]
public MyResponse GetCompanyContactsByID(int companyID, Guid? uid = null)
{
var response = new MyResponse();

    IQueryable<vwCompanyContact> contacts = 
coreDB.vwCompanyContacts.Where(con => (con.IDCompany == companyID) &&
                                      (uid == Guid.Empty || uid == CatalogNumber));

if(!contacts.Any()) response.ResponseMessage = "No Results found";
else
{
 response.ResponseMessage = String.Format("{0} contact is found", contacts.Count());
 response.MyContacts = contacts.ToList();
}

    return response ;
}