【问题标题】:Web API OData v4 only gives 404Web API OData v4 仅提供 404
【发布时间】:2016-03-04 18:21:02
【问题描述】:

我已经走投无路了,Google 的东西已经用完了。我不知道下面哪一段可能是错的?

localhost:29197/odata/Tests/ 返回 404

localhost:29197/odata?$metadata 返回 404(或者是 localhost:29197/odata/$metadata)

带或不带路由前缀的相同 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.Register 位于RouteConfig.RegisterRoutes 之前

Web.config 包含 &lt;modules runAllManagedModulesForAllRequests="true" /&gt;

【问题讨论】:

  • 您的代码在我的环境中运行良好(OWIN 在 OS X 上运行)。看起来是 IIS 或 Windows 配置问题。
  • 嗯,我在 localhost 上的 IIS Express 中托管
  • 顺便说一句,您的服务的正确 URI 是 http://localhost:29197/odata/$metadatahttp://localhost:29197/odata/Tests
  • 也不起作用,但很高兴确认,谢谢!
  • 您是否在应用程序启动事件中注册了 WebApi 配置? GlobalConfiguration.Configure(WebApiConfig.Register);,您的项目中还有其他非 odata API,它们是否有效?

标签: c# asp.net asp.net-web-api odata


【解决方案1】:

请确保您已在 global.asax.cs 的应用程序启动事件中注册了 WebApi 配置,如下所示 -

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

【讨论】:

  • 我有WebApiConfig.Register(GlobalConfiguration.Configuration);。另一个不认识。我在网上看到了这两种方式,但我不确定差异背后的原因是什么?
  • 哦...不同的是我没有安装Web API 2.2 5.x,我仍然有4.0。我以为安装 Microsoft ASP.NET Web API 2.2 for OData 会更新它.... idk 是笑、哭还是诅咒....谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 2014-10-08
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
相关资源
最近更新 更多