【问题标题】:Web API Method Returns Result Set Even If Parameter Doesn't Exist即使参数不存在,Web API 方法也会返回结果集
【发布时间】:2015-03-15 03:53:33
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 感谢您为我指明了正确的方向。这对我很有帮助 - 我将使用最终得到的代码来更新我的问题
    【解决方案2】:
    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 ;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-10
      • 2015-05-26
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多