即使参数不存在,Web API方法也会返回结果集

时间:2015-03-15 03:53:34

标签: c# linq entity-framework asp.net-web-api

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

[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/)我得到公司100的所有联系人的列表。然后如果我打电话(http://localhost:21598/DAL/api/company/100/contact/64077706-b7c9-e411-825d-28b2bd14ba94666)我只得到一条匹配GUID的记录。但是,如果我打电话(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);
}

2 个答案:

答案 0 :(得分:2)

您可以将参数声明为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;
}

答案 1 :(得分:-1)

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 ;
}