不能 return 对象
Cannot return object
我想 return 通过名称找到这个对象:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
控制器方法是:
[HttpGet]
[ODataRoute("Products/ProductService.GetByName(Name={name})")]
public IHttpActionResult GetByName([FromODataUri]string name)
{
Product product = _db.Products.Where(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
if (product == null)
{
return NotFound();
}
return Ok(product);
}
而WebApiConfig.Register()
方法是:
public static void Register(HttpConfiguration config)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
builder.Namespace = "ProductService";
builder.EntityType<Product>().Collection.Function("GetByName").Returns<Product>().Parameter<string>("Name");
config.MapODataServiceRoute(routeName: "ODataRoute", routePrefix: null, model: builder.GetEdmModel());
}
通过调用 http://http://localhost:52542/Products(1)
,我确实得到了预期 ID 为 1 的产品:
{
"@odata.context":"http://localhost:52542/$metadata#Products/$entity","Id":1,"Name":"Yo-yo","Price":4.95,"Category":"Toy"
}
但是当我调用 http://http://localhost:52542/Products/ProductService.GetByName(Name='yo-yo')
时,我确实可以调试到控制器函数中,结果是 returned 但我在浏览器中收到一个错误,说 An error has occurred.
.消息是 The 'ObjectContent 1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.
,内部异常是 The related entity set or singleton cannot be found from the OData path. The related entity set or singleton is required to serialize the payload.
.
这里有什么问题?
您的功能配置有问题。
您应该按如下方式调用来定义 return:
builder.EntityType<Product>().Collection.Function("GetByName").ReturnsFromEntitySet<Product>("Products").Parameter<string>("Name");
然后就可以了。谢谢
我想 return 通过名称找到这个对象:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
控制器方法是:
[HttpGet]
[ODataRoute("Products/ProductService.GetByName(Name={name})")]
public IHttpActionResult GetByName([FromODataUri]string name)
{
Product product = _db.Products.Where(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
if (product == null)
{
return NotFound();
}
return Ok(product);
}
而WebApiConfig.Register()
方法是:
public static void Register(HttpConfiguration config)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
builder.Namespace = "ProductService";
builder.EntityType<Product>().Collection.Function("GetByName").Returns<Product>().Parameter<string>("Name");
config.MapODataServiceRoute(routeName: "ODataRoute", routePrefix: null, model: builder.GetEdmModel());
}
通过调用 http://http://localhost:52542/Products(1)
,我确实得到了预期 ID 为 1 的产品:
{
"@odata.context":"http://localhost:52542/$metadata#Products/$entity","Id":1,"Name":"Yo-yo","Price":4.95,"Category":"Toy"
}
但是当我调用 http://http://localhost:52542/Products/ProductService.GetByName(Name='yo-yo')
时,我确实可以调试到控制器函数中,结果是 returned 但我在浏览器中收到一个错误,说 An error has occurred.
.消息是 The 'ObjectContent 1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.
,内部异常是 The related entity set or singleton cannot be found from the OData path. The related entity set or singleton is required to serialize the payload.
.
这里有什么问题?
您的功能配置有问题。 您应该按如下方式调用来定义 return:
builder.EntityType<Product>().Collection.Function("GetByName").ReturnsFromEntitySet<Product>("Products").Parameter<string>("Name");
然后就可以了。谢谢