My entity name is "Products" but in my WebApi the route is defined as "GetProducts" so in default it will query against /api/Products? instead of /api/GetProducts, is there any way to specify where it should fetch the data from the server if it can't find what i want in the cache.

So far i got this

manager.fetchEntityByKey("Products", productId, true)
    .then(fetchSucceeded)
    .fail(queryFailed);

This will call http://localhost:1990/breeze/Products?$filter=Id%20eq%201

But i want it to call http://localhost:1990/breeze/GetProducts?$filter=Id%20eq%201 instead

有帮助吗?

解决方案 2

You can change entity's default resourceName(that hits the breeze webapi method name) with

manager.metadataStore.setEntityTypeForResourceName("GetProducts", "Product")

But be sure the code above is executed when promise of manager.fetchMetadata() is resolved:

manager.fetchMetadata().then(function(){
    manager.metadataStore.setEntityTypeForResourceName(...);
})

If setEntityTypeForResourceName is called before metadata has loaded you will get an error, i.e

Unable to locate 'entityTypeName' of: Product

其他提示

There's also another option to specify this on the server. With WebApi 2, you can use the RouteAttribute on your controller action to customize the routing.

See http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

EDIT:

On the client, the Breeze EntityType has a property called defaultResourceName which you can modify as well to tell Breeze which default resource name to use if only an entity type name is specified.

var productType = metadataStore.getEntityType('Products');
productType.defaultResourceName = 'GetProducts';

//Will create fetch request to breeze/GetProducts
manager.fetchEntityByKey('Products', productId);

You can set the Resource for the given entity type which would hit the resource instead -

    manager.metadataStore.setEntityTypeForResourceName('GetProducts', 'Products');

But as a note it seems like a design flaw to name your entity Products instead of Product.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top