C# Web API方法返回结果集,即使参数不为';不存在
我一直在玩弄路由和可选参数,我遇到了一个问题,不管从数据库中得到什么结果。以下是我在控制器中的方法:C# Web API方法返回结果集,即使参数不为';不存在,c#,linq,entity-framework,asp.net-web-api,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.vwCompanyContac
[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;
}
[路由(“{companyID:int}/contact/{uid?}”)]
[HttpGet]
公共IQueryable GetCompanyContactsByID(int companyID,Guid?uid=null)
{
IQueryable contact=coreDB.vwCompanyContacts.Where(con=>con.IDCompany==companyID);
如果(uid!=null)
contact=contact.Where(con=>con.CatalogNumber==uid);
回接;
}
所以基本上如果call()我会得到100公司所有联系人的列表。然后,若调用()则只会得到一条GUID匹配的记录。但是如果我调用()我会再次得到所有联系人的列表
我宁愿接收一个空的结果集,或者返回一条消息说找不到结果。我不知道如何处理这个问题,因为我对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);
}
[路由(“{companyID:int}/contact/{uid?}”)]
[HttpGet]
公共IHttpActionResult GetCompanyContactByID(int companyId,字符串uid=null)
{
液体接触
=coreDB.vwCompanyContacts.Where(con=>con.IDCompany==companyID);
如果(!string.IsNullOrEmpty(uid))
{
Guid UID值;
if(Guid.TryParse(uid,out-uidValue))
contact=contact.Where(con=>con.CatalogNumber==uidValue);
其他的
返回状态码(HttpStatusCode.NoContent);;
}
返回Ok(联系);
}
您可以将参数声明为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;
}
[路由(“{companyID:int}/contact/{uid?}”)]
[HttpGet]
公共IQueryable GetCompanyContactsByID(int companyID,字符串uid=null)
{
Guid?uidValue=null;
如果(!string.IsNullOrEmpty(uid)和&!Guid.TryParse(uid,out-uidValue))
抛出新的ArgumentException(“uid”);
液体接触
=coreDB.vwCompanyContacts.Where(con=>con.IDCompany==companyID);
if(uidValue.HasValue)
contact=contact.Where(con=>con.CatalogNumber==uidValue.Value);
回接;
}
或者可以有两个单独的重载:
[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;
}
[路由(“{companyID:int}/contact/”)
[HttpGet]
公共IQueryable GetCompanyContactsByID(int companyID)
{
液体接触
=coreDB.vwCompanyContacts.Where(con=>con.IDCompany==companyID);
回接;
}
[路由(“{companyID:int}/contact/{uid}”)]
[HttpGet]
公共IQueryable GetCompanyContactsByID(int companyID,Guid uid)
{
液体接触
=coreDB.vwCompanyContacts.Where(con=>con.IDCompany==companyID
&&con.CatalogNumber==uid);
回接;
}
公共类MyResponse
{
公共IList myContacts{set;get;}
公共字符串响应消息{set;get;}
}
[路由(“{companyID:int}/contact/{uid?}”)]
[HttpGet]
公共MyResponse GetCompanyContactsByID(int companyID,Guid?uid=null)
{
var response=新的MyResponse();
IQueryable联系人=
coreDB.vwCompanyContacts.Where(con=>(con.IDCompany==companyID)&&
(uid==Guid.Empty | | uid==CatalogNumber));
如果(!contacts.Any())response.ResponseMessage=“未找到结果”;
其他的
{
response.ResponseMessage=String.Format(“{0}找到联系人”,contacts.Count());
response.MyContacts=contacts.ToList();
}
返回响应;
}
感谢您为我指明了正确的方向。。这很有帮助-我将用最终的代码更新我的问题
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 ;
}