ASP.NET Odata Web API CRUD操作不起作用

时间:2013-11-07 07:55:26

标签: c# web-services rest asp.net-mvc-4 odata

我花了大约2天的时间来设置我的Odata Web API项目,该项目是在一个简单的Asp.net mvc4项目之前。

但我仍然没有成功操作CRUD操作。 它说:

<m:message xml:lang="en-US">
No HTTP resource was found that matches the request URI 'http://localhost:53208/odata/Product'.
</m:message>
<m:innererror>
<m:message>
No action was found on the controller 'Product' that matches the request.
</m:message>
<m:type/>
<m:stacktrace/>
</m:innererror>

这意味着它到达控制器但没有找到操作或我的路由设置中存在一些问题。

我在WebApiConfig中有以下内容:

config.Routes.MapODataRoute("OData","odata",GetEdmModel());

我的getEdmModel方法:

private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Product");
        builder.EntitySet<OfferedProduct>("OfferedProduct");


        IEdmModel model = builder.GetEdmModel();

        return model;
    }

我的控制器是这样的:

public class ProductController : EntitySetController<Product,int>
{
    private OfferAssistantDbContext db = new OfferAssistantDbContext();
    List<Product> Products = new OfferAssistantDbContext().Products.ToList();
    // GET api/Product
    [Queryable(PageSize = 10)]
    public override IQueryable<Product> Get()
    {
        return Products.AsQueryable();
    }

    // GET api/Product/5
    public Product GetProduct([FromODataUri] int id)
    {
        return Products[id];
    }
    /// and so on... but for this time lets work only on GET operation 

现在我在浏览器中写这个:

http://localhost:53208/odata/Product

它说我上面显示的错误..

请指导我问题在哪里?

1 个答案:

答案 0 :(得分:1)

我相信您的控制器需要从ODataController继承:

public class ProductController : ODataController