【发布时间】:2015-01-07 23:23:41
【问题描述】:
我已经开始在我的 WebAPi2 项目中包含 OData(目前托管在我的开发机器上的 IIS8 Express 中)。我的 OData 配置类如下所示:
public class ODataConfig
{
private readonly ODataConventionModelBuilder modelBuilder;
public ODataConfig()
{
modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Category>("Category");
}
public IEdmModel GetEdmModel()
{
return modelBuilder.GetEdmModel();
}
}
然后我在我的 WebApiConfig 类中添加了以下内容:
ODataConfig odataConfig = new ODataConfig();
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "MyServer/OData",
model: odataConfig.GetEdmModel(),
defaultHandler: sessionHandler
);
从一个基本的控制器和一个动作开始,像这样:
public class CategoryController : ODataController
{
[HttpGet]
public IHttpActionResult Get([FromODataUri] int key)
{
var entity = categoryService.Get(key);
if (entity == null)
return NotFound();
return Ok(entity);
}
}
然后,在我的 HttpClient 中,请求 url 如下所示: MyServer/OData/Category(10)
但是,我收到以下错误:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/MyServer/OData/Category(10)'.","MessageDetail":"No type was found that matches the controller named 'OData'."}
我在这里错过了什么?
编辑
如果我将 routePrefix 设置为 null 或 'odata' 并相应地更改我的请求 URL,则请求可以正常工作。所以这意味着我不能有像“myServer/odata”这样的路由前缀。
这是 OData 标准命名约定吗?如果是,是否可以覆盖?
【问题讨论】:
标签: c# asp.net-web-api odata