如何让我的EntitySet Controller对我的路线可见?

时间:2013-10-08 23:22:12

标签: asp.net asp.net-mvc

我创建了一个如下所示的EntitySetController:

   public class OrdersController : EntitySetController<Order,Guid>
{
    private readonly PizzaCompanyEntities _context = Factories.DataFactory.GetPizzaContext();

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _context.Dispose();
        }
        base.Dispose(disposing);
    }

    public override IQueryable<Order> Get()
    {
        return _context.Orders;
    }

    protected override Order GetEntityByKey(Guid key)
    {
        var result = _context.Orders.FirstOrDefault(o => o.Id == key);
        if (result == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return result;
    }
}

在现有的MVC 4 Web应用程序中。 我按如下方式配置路由:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapODataRoute("PizzaApi", "odata", GetImplicitEdm());
    }

    private static IEdmModel GetImplicitEdm()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<Order>("Orders");
        builder.EntitySet<Pizza>("Pizzas");
        builder.EntitySet<Pizzas_To_Orders>("PizzasToOrders");
        builder.EntitySet<Size>("Sizes");
        builder.EntitySet<Status>("Statuses");
        builder.EntitySet<Pizzas_To_Toppings>("PizzasToToppings");

        return builder.GetEdmModel();
    }
}

按如下方式执行配置:

    public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

但是当我在http://localhost:29064/odata/Orders处执行我的路线时,我收到了404消息“未找到路径/ odata / Orders的控制器或未实现IController。

我无法弄清楚我错过了什么,以便注册路由和控制器运行。我从头开始做了类似的应用程序并没有遇到这个麻烦。

如何让OData路线正常工作?

0 个答案:

没有答案