Breeze使用withParameters进行查询并展开,而不是展开
在获取实体的过程中,我需要在服务器端实现一些业务逻辑,以确保用户对正在检索的项具有权限 为了实现这一点,我的客户端breeze查询代码更改如下:Breeze使用withParameters进行查询并展开,而不是展开,breeze,asp.net-web-api2,durandal-2.0,Breeze,Asp.net Web Api2,Durandal 2.0,在获取实体的过程中,我需要在服务器端实现一些业务逻辑,以确保用户对正在检索的项具有权限 为了实现这一点,我的客户端breeze查询代码更改如下: var query = breeze.EntityQuery .from(theUrl) .expand("RelatedEntity") .where(breeze.Predicate.create("id", "==", id)); [HttpGet] [BreezeQueryable] public IHttpActio
var query = breeze.EntityQuery
.from(theUrl)
.expand("RelatedEntity")
.where(breeze.Predicate.create("id", "==", id));
[HttpGet]
[BreezeQueryable]
public IHttpActionResult FindById(int id)
{
var userId = HttpContext.Current.User.Identity.GetUserId();
var hasPermission = CheckPermission(id, userId); // some implementation ...
if (hasPermission) {
var myEntity = _uow.MyEntityRepo.GetById(id);
return Ok(myEntity)
} else {
return NotFound();
}
}
致:
在服务器端,我的控制器操作更改为:
[HttpGet]
[BreezeQueryable]
public IQueryable<MyEntity> MyEntities()
{
return _uow.MyEntityRepo.All();
}
我可以看到查询与过滤器的连接:
http://localhost:42789/breeze/MyEntity/FindById?$expand=RelatedEntity&id=1002
但是,RelatedEntity
未定义。当使用实体查询
,但不使用带参数的,相关实体会很好地展开,并在结果集中可用
谢谢withParameters调用需要引用IQueryable端点,并且还需要在客户端上命名该端点。此外,如果要调用expand,则返回的queryable必须支持EF“Include”操作。例如,如果您有这样一个服务器端方法
[HttpGet]
public IQueryable<Customer> CustomersStartingWith(string companyName) {
return ContextProvider.Context.Customers.Where(c => c.CompanyName.StartsWith(companyName));
}
注意“CustomersStartingWith”作为“from”子句的参数 谢谢,杰。这对我来说是可行的,但我看到的唯一缺点是我无法使用HttpActionResult将404或401正确地通信回客户端。不过我可以解决这个问题。实际上,您可以通过在服务器方法[HttpGet]中抛出HttpResponseException来返回401或404。[HttpGet]public IQueryable Customers WithHttpError(){var responseMsg=new HttpResponseMessage(HttpStatusCode.NotFound);responseMsg.Content=new StringContent(“自定义错误消息”);responseMsg.ReasonPhrase=“自定义原因”抛出新的HttpResponseException(responseMsg);}
[HttpGet]
public IQueryable<Customer> CustomersStartingWith(string companyName) {
return ContextProvider.Context.Customers.Where(c => c.CompanyName.StartsWith(companyName));
}
var q = EntityQuery.from("CustomersStartingWith")
.withParameters({ companyName: "C" })
.expand("orders");