Odata使用Knock out Js路由和访问它

时间:2014-02-04 20:40:54

标签: c# javascript asp.net-mvc-4 knockout.js odata

我使用OData和Knockout Js在Asp.Net Mvc4 Internet Application上开发了一个应用程序。我的OData中的路由如下

WebApiConfig.cs

ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.EntitySet<Product>("Products");
            var model = modelBuilder.GetEdmModel();

            config.Routes.MapODataRoute(routeName: "Odata", routePrefix: "odata", model: model);

和Knockout Js如下

 $.getJSON('odata/Products', function (data) {
        self.products(ko.utils.arrayMap(data.value, function (product) {
            var obsProduct = {
                Id: product.Id,
                ProductName: ko.observable(product.ProductName),

                Edit: ko.observable(false),
                Locked: ko.observable(product.Locked)
            }
            self.watchModel(obsProduct, self.modelChanged);
            return obsProduct;
        }));
        self.loading(false);
    });
}

但是在运行应用程序时,它抛出了一个异常,如下所示

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:57044/Home/odata/Products

但是当我手动输入网址http://localhost:57044/odata/Products时,是显示包含所有产品列表的元数据

但是当我输入http://localhost:57044/Home/odata/Products(这是与上面不同的网址)时,它会抛出404错误。我该如何解决?

1 个答案:

答案 0 :(得分:3)

  

/主页/的OData /产品

- &GT;这是问题,HOME,你的网址不正确

$.getJSON('/odata/Products', function (data) {
    self.products(ko.utils.arrayMap(data.value, function (product) {
        var obsProduct = {
            Id: product.Id,
            ProductName: ko.observable(product.ProductName),

            Edit: ko.observable(false),
            Locked: ko.observable(product.Locked)
        }
        self.watchModel(obsProduct, self.modelChanged);
        return obsProduct;
    }));
    self.loading(false);
});