简体   繁体   中英

Breeze fails to resolve the web api controller method

I am using Breeze on both my client and server with a sql express database.

My server code :

[Authorize]
[BreezeController]
public class DataController : ApiController{

   [HttpGet]
   public IQueryable<Product> Products()
   {
     ...
   }
}

My client code:

var getProducts = function () {
        var query = entityQuery.from('Products');

        return manager.executeQuery(query)
               .then(querysucceeded)
               .fail(queryfailed);
        ...
    };

This works fine. However, the documentation says that the parameter passed to EntityQuery.from(...) should be one of the breeze web api controller methods. So I would expect adding the following to the server:

   [HttpGet]
   public IQueryable<Product> TestMethod()
   {
     ...
   }

And this to the client:

var getProjects = function () {
        var query = entityQuery.from('TestMethod');

        return manager.executeQuery(query)
               .then(querysucceeded)
               .fail(queryfailed);
        ...
    };

I expected this to work but it doesn't :( I get the following error on the console:

Uncaught Error: Cannot find an entityType for resourceName: 'TestMethod'. Consider adding an 'EntityQuery.toType' call to your query or calling the MetadataStore.setEntityTypeForResourceName method to register an entityType for this resourceName.

The Breeze client needs to know the EntityType that is being returned corresponding to each "resourceName". It knows this for the "Products" resource because of Metadata, where this resourceName is paired with the "Product" entityType, but it has no idea what "TestMethod" returns.

So your fix is to either call

manager.setEntityTypeForResourceName("TestMethod", "Product");

before executing your query. Or you can change your query to use the 'toType' method.

var query = entityQuery.from('TestMethod').toType("Product");
return manager.executeQuery(query)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM