ASP.NET WebAPI 2.1
以下是我的情景:
如何使用CustomerID过滤所有发票,订单等的Web API调用?
我经常搜索,找不到任何样品。如果有人能指出正确的方向和/或示例代码,我真的很感激。
以下是我的控制器:
public class invoicesController : ApiController
{
private myEntities db new db1Entities();
// GET api/invoices
public IQueryable<invoices> Getinvoices()
{
// here (I guess) I need to add a filter by CustomerID
// first get CustomerID from logged In User
return db.invoices;
}
// GET api/invoices/5
[ResponseType(typeof(invoices))]
public IHttpActionResult Getinvoices(int id)
{
invoices invoices_by_id = db.invoices.Find(id);
if (invoices_by_id == null)
{
return NotFound();
}
return Ok(invoices_by_id);
}
}
行。我得到了它的工作,但我认为必须有更好的方式来访问用户属性......任何人?
// Instantiate the UserManager in ASP.Identity system to look up the user in the system
// ApplicationUser class is defined in IdentityModels.cs, same as ApplicationDbContext
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
//Get the User object
var currentUser = manager.FindById(User.Identity.GetUserId());
//Get the profile information about the user
int? userCustomerID = currentUser.customerID;
在控制器中我刚改变了最后一行:
return View(db.invoices.Where(i => i.CustomerID == userCustomerID).ToList());