Web API:Odata路由始终返回404响应 [英] Web API: Odata route always returning a 404 response

查看:76
本文介绍了Web API:Odata路由始终返回404响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OData的新手.我已经建立了一个ASP.NET Web API控制器,如下所示:

I am new to OData. I have built an ASP.NET Web API controller as shown below:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web.OData.Routing;

namespace HelloWebApi.Controllers
{
  public class TestsController : ODataController
  {
    ProductsContext db = new ProductsContext();
    private bool TestExists(int key)
    {
      return db.tests.Any(p => p.key== key);
    }
    protected override void Dispose(bool disposing)
    {
      db.Dispose();
      base.Dispose(disposing);
    }

    [EnableQuery]
    public IQueryable<test> Get()
    {
      return db.tests;
    }
  }
}

模型如下所示:

public class Test
{
  [Key]
  public int key { get; set; }
  public string aaa { get; set; }
}

我还配置了RouteConfigODdataConfigWebApiConfig,如下所示:

I have also configured the RouteConfig, ODdataConfig, and WebApiConfig as shown below:

public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.Ignore("{resource}.axd/{*pathInfo}");
    routes.MapHttpRoute(
      name: "Default",
      routeTemplate: "{controller}/{action}/{id}"
    );
  }
}

public class ODataConfig
{
  public static void Register(HttpConfiguration config)
  {
    // Web API routes
    config.MapHttpAttributeRoutes();
    ODataModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Test>("Tests");
    config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
  }
}

public static class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
    );           

  }
}

以及global.asax文件:

public class WebApiApplication : System.Web.HttpApplication
{
  protected void Application_Start()
  {
    GlobalConfiguration.Configure(config =>
    {
      ODataConfig.Register(config); //this has to be before WebApi
      WebApiConfig.Register(config);
    });
    //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
  }
}

为了解决这个问题,我尝试了一些修改.但是我一直收到HTTP 404 Not Found响应.我还尝试将[ODataRoute]属性显式添加到操作方法名称中.这样做时,我得到的是HTTP 406不可接受的响应.

I tried making a number of modifications in order to resolve this. But I am consistently getting an HTTP 404 Not Found response. I also tried explicitly adding an [ODataRoute] attribute to the action method name; when doing that I instead get an HTTP 406 Not Acceptable response.

我要配置的URL是:

http://localhost:6701/odata/tests/

其中odata是后缀,tests是控制器名称.请指出我做错了.

Where odata is the suffix and tests is the controller name. Please point out what I am doing wrong.

推荐答案

The routes configured by the ODataConventionModelBuilder are case-sensitive. In your code, you have defined:

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

基于此,端点将为 http://localhost:6701/odata/Tests/(请注意测试"中的大写字母"T").

Based on this, the endpoint will be http://localhost:6701/odata/Tests/ (note the upper-case "T" in "Tests").

这是设计使然,以保持与OData规范的兼容性.

This is by design, in order to maintain compatibility with the OData specification.

也就是说,截至 Web API OData 5.4 ,您可以选择使用HttpConfiguration类的EnableCaseInsensitive()方法启用不区分大小写的路由.例如,您可以在ODataConfig.Register()方法中添加:

That said, as of Web API OData 5.4, you can optionally enable case-insensitive routes using the HttpConfiguration class's EnableCaseInsensitive() method. E.g., in your ODataConfig.Register() method you could add:

config.EnableCaseInsensitive(caseInsensitive: true);

有关更多信息,请参见不区分大小写的基本支持在Microsoft的用于OData V4文档的ASP.NET Web API 下.

For more information, see Basic Case Insensitive Support under Microsoft's ASP.NET Web API for OData V4 Docs.

这篇关于Web API:Odata路由始终返回404响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆