我正在尝试配置一些odata导航,但无法看到这里的问题。
配置:
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
var entitySetConfiguration = modelBuilder.EntitySet<Catalog>("Catalog");
entitySetConfiguration.EntityType.Ignore(t => t.OutboundCatalogItems);
entitySetConfiguration.EntityType.Ignore(t => t.OutboundColumns);
entitySetConfiguration.EntityType.Ignore(t => t.OutboundProcessor);
entitySetConfiguration.EntityType.Ignore(t => t.OutboundProcessorID);
entitySetConfiguration.EntityType.Ignore(t => t.RelationshipId);
entitySetConfiguration.EntityType.Ignore(t => t.CatalogConfigurationProperties);
var model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "odata", model);
config.EnableQuerySupport();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "apicontrollers/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
注册
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_EndRequest(object sender, EventArgs e)
{
Helpers.DataAccess.DisposePDBUnitOfWork();
}
}
控制器
public class CatalogController : EntitySetController<Catalog, int>
{
private readonly PDBCoreUnitOfWork _unitOfWork;
public CatalogController()
{
_unitOfWork = new PDBCoreUnitOfWork(0);
}
public override IQueryable<Catalog> Get()
{
return _unitOfWork.Repository<Catalog>().Query().Get();
}
protected override void Dispose(bool disposing)
{
_unitOfWork.Dispose();
base.Dispose(disposing);
}
}
脚本
<script type="text/javascript">
var catalogApi = "/odata/Catalog";
$.getJSON(catalogApi)
.done(function(json) {
console.log("Json data: " + json);
})
.fail(function(jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
</script>
使用fiddler localhost:port / odata返回404,localhost:port / odata / Catalog
我必须在这里遗漏一些筹款。Unit of work with oData
更新 当我评论以下内容时:
protected void Application_Start()
{
//AreaRegistration.RegisterAllAreas();
//RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
//GlobalConfiguration.Configure(ODataConfig.Register);
}
和
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
//var entitySetConfiguration = modelBuilder.EntitySet<Catalog>("Catalog");
//entitySetConfiguration.EntityType.Ignore(t => t.OutboundCatalogItems);
//entitySetConfiguration.EntityType.Ignore(t => t.OutboundColumns);
//entitySetConfiguration.EntityType.Ignore(t => t.OutboundProcessor);
//entitySetConfiguration.EntityType.Ignore(t => t.OutboundProcessorID);
//entitySetConfiguration.EntityType.Ignore(t => t.RelationshipId);
//entitySetConfiguration.EntityType.Ignore(t => t.CatalogConfigurationProperties);
var model = modelBuilder.GetEdmModel();
//config.MapHttpAttributeRoutes();
config.Routes.MapODataRoute("odata", "odata", model);
config.EnableQuerySupport();
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
// );
}
}
键入localhost / port / odata返回:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="http://localhost:45837/odata">
<workspace>
<atom:title type="text">Default</atom:title>
</workspace>
</service>
但是当我在odata之后添加Catalog
时,它会抛出404找不到。仍然迷失,但取得了进展。
答案 0 :(得分:1)
我需要交换注册顺序
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(ODataConfig.Register);
}
并改变了这个
public class CatalogController : EntitySetController<Catalog, int>
{
....
}
到此
public class CatalogController : OdataController
{
....
}
答案 1 :(得分:1)
在新的WebAPI版本中删除了EntitySetController和AsyncEntitySetController。您始终可以使用&#34; public class {EntitySetName}Controller : ODataController
作为控制器。
顺便说一下,您可以参考以下博客,了解有关使用OData V4的WebApi 2.2的新用法: