正在使用breeze js和asp.net odata控制器编写角度应用程序。 Breeze JS成功地第一次调用服务器获取元数据,但没有进行第二次调用以实际获取数据。我在Chrome中看到的是
XHR finished loading: GET "http://localhost:31549/Odata/ClientInvestments/$metadata".
返回
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<Schema Namespace="Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.DataContracts" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="ClientInvestment">
<Property Name="InvestmentNumber" Type="Edm.String" />
<Property Name="Initials" Type="Edm.String" />
<Property Name="InvestorFirstName" Type="Edm.String" />
<Property Name="InvestorLastName" Type="Edm.String" />
<Property Name="InvestorNumber" Type="Edm.String" />
<Property Name="BrokerCode" Type="Edm.String" />
<Property Name="UnclearedUnits" Type="Edm.Decimal" />
<Property Name="UnclearedMarketValue" Type="Edm.Decimal" />
<Property Name="BlockedUnits" Type="Edm.Decimal" />
<Property Name="BlockedMarketValue" Type="Edm.Decimal" />
<Property Name="TotalUnits" Type="Edm.Decimal" />
<Property Name="NavDate" Type="Edm.DateTime" />
<Property Name="NavCents" Type="Edm.Decimal" />
<Property Name="CurrentValue" Type="Edm.Decimal" />
<Property Name="FundName" Type="Edm.String" />
<Property Name="FundCode" Type="Edm.String" />
<Property Name="ProductName" Type="Edm.String" />
<Property Name="ProductCode" Type="Edm.String" />
<Property Name="ProductType" Type="Edm.String" />
<Property Name="EntityCode" Type="Edm.String" />
<Property Name="InvestmentType" Type="Edm.String" />
</EntityType>
<EntityType Name="ClientTransaction">
<Property Name="TransactionId" Type="Edm.Int32" />
<Property Name="BrokerCode" Type="Edm.String" />
<Property Name="InvestorNumber" Type="Edm.String" />
<Property Name="InvestmentNumber" Type="Edm.String" />
<Property Name="TransactionDate" Type="Edm.DateTime" />
<Property Name="Description" Type="Edm.String" />
<Property Name="GrossAmount" Type="Edm.Decimal" />
<Property Name="AgentFee" Type="Edm.Decimal" />
<Property Name="VatOnFees" Type="Edm.Decimal" />
<Property Name="TotalAgentFees" Type="Edm.Decimal" />
<Property Name="NetAmount" Type="Edm.Decimal" />
<Property Name="NavPrice" Type="Edm.Decimal" />
<Property Name="Units" Type="Edm.Decimal" />
<Property Name="Wauc" Type="Edm.Decimal" />
</EntityType>
<EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
<EntitySet Name="ClientInvestments" EntityType="Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.DataContracts.ClientInvestment" />
<EntitySet Name="ClientTransactions" EntityType="Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.DataContracts.ClientTransaction" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
然后没什么
我的wep api配置如此
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapODataServiceRoute("Odata", "Odata/ClientInvestments", CreateClientInvestmentEdmModel());
config.Routes.MapODataServiceRoute("OdataCommission", "Odata/Commission", CreateCommissionEdmModel());
config.Routes.MapODataServiceRoute("OdataAssetsUnderManagement", "Odata/AssetsUnderManagement", CreateAssetsUnderManagementEdmModel());
config.Routes.MapHttpRoute(
name: "Reports",
routeTemplate: "api/reports/{action}",
defaults: new { controller = "Reports" }
);
config.Routes.MapHttpRoute(
name: "SecondaryMarket",
routeTemplate: "api/secondarymarket/{action}",
defaults: new { controller = "SecondaryMarket" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.Add(new CsvMediaTypeFormatter());
config.Formatters.Add(new ExcelMediaTypeFormatter());
config.MessageHandlers.Add(new LogRequestHandler());
config.Filters.Add(new HandleErrorFilter());
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;
}
public static IEdmModel CreateClientInvestmentEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.Namespace = typeof(Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.DataContracts.ClientInvestment).Namespace;
builder.EntitySet<Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.DataContracts.ClientInvestment>(Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.ClientInvestmentsServiceConstants.ClientInvestmentsQuery);
builder.EntitySet<Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.DataContracts.ClientTransaction>(Cadiz.Services.CadizOnline.Contracts.ClientInvestmentsService.ClientInvestmentsServiceConstants.ClientTransactionsQuery);
return builder.GetEdmModel();
}
public static IEdmModel CreateCommissionEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.Namespace = typeof(Cadiz.Services.CadizOnline.Contracts.CommissionService.DataContracts.CommissionDetail).Namespace;
builder.EntitySet<Cadiz.Services.CadizOnline.Contracts.CommissionService.DataContracts.CommissionDetail>(Cadiz.Services.CadizOnline.Contracts.CommissionService.CommissionServiceConstants.CommissionDetailsQuery);
builder.EntitySet<Cadiz.Services.CadizOnline.Contracts.CommissionService.DataContracts.CommissionSummary>(Cadiz.Services.CadizOnline.Contracts.CommissionService.CommissionServiceConstants.CommissionSummaryQuery);
return builder.GetEdmModel();
}
public static IEdmModel CreateAssetsUnderManagementEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.Namespace = typeof(Cadiz.Services.CadizOnline.Contracts.AssetsUnderManagementService.DataContracts.ClientAsset).Namespace;
builder.EntitySet<Cadiz.Services.CadizOnline.Contracts.AssetsUnderManagementService.DataContracts.ClientAsset>(Cadiz.Services.CadizOnline.Contracts.AssetsUnderManagementService.AssetsUnderManagementServiceConstants.AssetsUnderManagementQuery);
return builder.GetEdmModel();
}
}
我的javascript配置如此
breeze.config.initializeAdapterInstances({
dataService: "OData"
});
var dataService = new breeze.DataService({
serviceName: '/Odata/ClientInvestments'
});
var manager = new breeze.EntityManager({ dataService: dataService });
var query = new breeze.EntityQuery('ClientInvestments');
manager.executeQuery(query).then(function(data){
var results = data;
});
我的odata控制器看起来像
namespace Cadiz.Web.CadizOnline.Controllers.Odata.ClientInvestments
{
[Authorize]
public class ClientInvestmentsController : ODataController
{
#region Private Members
private readonly IClientInvestmentsService dataContext;
#endregion
#region Constructors
public ClientInvestmentsController(IClientInvestmentsService dataContext)
{
this.dataContext = dataContext;
}
#endregion
#region Methods
[EnableQuery(MaxOrderByNodeCount = 1)]
[ClaimsAuthorise(CadizSecurityConstants.Operations.Read, CadizSecurityConstants.Resources.Client)]
public IHttpActionResult Get()
{
var query = dataContext.ClientInvestments;
return Ok(query);
}
#endregion
#region Virtual Methods
protected override void Dispose(bool disposing)
{
if (disposing)
{
dataContext.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
}
答案 0 :(得分:1)
EntityManager executeQuery调用是异步的并返回一个promise,所以你需要像这样调用它:
manager.executeQuery(query).then(function(data) {
var results = data.results;
}
答案 1 :(得分:1)
不确定这是否有所不同,但您可以尝试编写如下查询:
var query = breeze.EntityQuery
.from("ClientInvestments");
manager.executeQuery(query);
或者像这样:
new breeze.EntityQuery()
.from("ClientInvestments")
.using(manager)
.execute();
我也不确定OData
是否是正确的数据服务,或者您是否应该使用webApiOData
。
答案 2 :(得分:0)
检查您的cors配置,如果未正确配置cors,有时微风会静默失败