2016-03-04 55 views
-1

我在我的绳索末端,我已经用尽了谷歌。我不知道下面的哪一部分可能是错的?Web API OData v4只提供了404

本地主机:29197 /的OData /测试/返回404

本地主机:29197/OData的$元返回404(或者是本地主机:29197 /的OData/$元数据)?

同404带或不带路由前缀。

控制器:

namespace MvcApplication.Api 
{ 
    public class TestsController : ODataController 
    { 
     [EnableQuery] 
     public IQueryable<Test> Get() 
     { 
      return new List<Test>() {new Test() {Id = 1}}.AsQueryable(); 
     } 
    } 
} 

WebApiConfig:

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     config.MapHttpAttributeRoutes(); 

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

     config.MapODataServiceRoute(routeName: "odata", routePrefix: "odata", model: GetEdmModel()); 
    } 

    private static IEdmModel GetEdmModel() 
    { 
     var builder = new ODataConventionModelBuilder(); 

     builder.EntitySet<Test>("Tests"); 

     var model = builder.GetEdmModel(); 
     return model; 
    } 
} 

WebApiConfig.RegisterRouteConfig.RegisterRoutes

在Web.config包含<modules runAllManagedModulesForAllRequests="true" />

+1

您的代码在我的环境中正常工作(OWIN在OS X上运行)。看起来像它的IIS或Windows配置问题。 – lencharest

+0

嗯,我在IIS Express上托管本地主机 – RJB

+0

顺便说一句,您的服务的正确URI是'http:// localhost:29197/odata/$ metadata'和'http:// localhost:29197/odata/Tests'。 – lencharest

回答

0

只要确保你已注册的WebAPI conffig申请中的global.asax.cs像下面开始事件 -

public class WebApiApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     GlobalConfiguration.Configure(WebApiConfig.Register); 
    } 
} 
+0

我有'WebApiConfig.Register(GlobalConfiguration.Configuration);'。另一个不被认可。我在网上看到了这两种方式,但我不确定背后有什么差异? – RJB

+0

呃...区别在于我没有'Web API 2.2' 5.x安装,我还有4.0。我以为安装'微软ASP.NET API API的OData'会更新.... idk是否笑,哭或诅咒....谢谢! – RJB