使用breeze和WebApi获取System.InvalidOperationException异常

时间:2013-10-12 13:49:44

标签: asp.net-mvc angularjs asp.net-web-api breeze

我很陌生。我已经下载了AngularBreeze的模板并试图创建一个示例应用程序,但我被困在Breeze WebApi Controller上。

BreezeController]
public class ProductsBreezeController : ApiController
{
    private readonly ProductRepository context;

    public ProductsBreezeController()
    {
        context = new ProductRepository();
    }

    [HttpGet]
    public string Metadata()
    {
        return context.Metadata();
    }

    //// GET api/productsbreeze
    public IQueryable<Product> GetAllProducts()
    {
        return context.TodoLists;
    }
}

public class ProductRepository : EFContextProvider<SampleEntities>
{
    public DbQuery<Product> TodoLists
    {
        get { return Context.Products; }
    }
}

异常消息

找到了与请求匹配的多个操作:类型AngularWebApi.ApiControllers类型AngularWebApi.ApiControllers.ProductsBreezeController System.Linq.IQueryable`1 [AngularWebApi.DataAccess.Model.Product] GetAllProducts()上的System.String元数据()。 ProductsBreezeController

ExceptionType :“System.InvalidOperationException”

2 个答案:

答案 0 :(得分:0)

您需要将breezewebapiconfig.cs设置为接受动作参数。目前你可能只有一个控制器。

打开appstart文件夹和BreezeWebApiConfig.cs并将其添加到那里(应该会看到类似的内容) -

Breeze/{controller}/{action}/{id} 

你需要在那里添加动作部分

修改

在您的问题中,它清楚地显示了该控制器操作的路径是api / productsbreeze。如果这是你正在击中的路线,那么你需要调整该路线以接受一个动作。如果是您尝试点击的Breeze路线,则在操作

上添加HttpGet控制器属性
//// GET api/productsbreeze
[HttpGet]
public IQueryable<Product> GetAllProducts()
{
    return context.TodoLists;
}

当然,你需要确保你的BreezeWebApiConfig也在Global.asax中注册。

答案 1 :(得分:0)

请求URL应与Breeze Api配置匹配。

服务器端配置

GlobalConfiguration.Configuration.Routes.MapHttpRoute("BreezeApi", "breeze/{controller}/{action}");

客户端

var manager = new breeze.EntityManager("/breeze/ProductsBreeze");