Odata收到“路径控制器未找到或未实现IController”

时间:2013-11-17 01:20:51

标签: c# asp.net-mvc odata

我对Odata和Web Api有点新意。我从Fiddler的get请求中得到了这个错误:

未找到路径'/ odata / Unit'的控制器或未实现IController

public class UnitController : EntitySetController<Unit, Guid>
{
    public DEVEntities _Context { get; set; }

    public UnitController()
    {
        this._Context = new DEVEntities();
    }

    public override IQueryable<Unit> Get()
    {
        return _Context.UnitSet;
    }

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

我的WebApiConfig.cs

    public static void Register(HttpConfiguration config)
    {
        ODataModelBuilder builder = new ODataModelBuilder();
        builder.EntitySet<Unit>("Units");
        config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

I can get http://server/odata/$metadata with no problems but when I try:

but when I try http://server/odata/Unit I get:

[HttpException]: The controller for path &#39;/odata/Unit&#39; was not found or does  not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
    at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

请帮助我非常沮丧。

2 个答案:

答案 0 :(得分:0)

我认为你的UnitController或EntitySetController没有继承ApiController

答案 1 :(得分:0)

我找到了答案,我必须将ODataModelBuilder更改为ODataConventionModelBuilder。

解决了这个问题。

并且应该将“单位”更改为“单位”以映射到正确的控制器。