2016-12-19 61 views
0

I”试图调用,接收来自URI的参数,如下面的web API的OData控制器的方法:网络API的OData V4 FromODataUri总是返回404未找到

// GET /odata/People(3) 
    public SingleResult<Person> Get([FromODataUri] int key) 
    { 
     return SingleResult.Create(DemoDataSources.Instance.People.Where(p => p.ID == key.ToString()).AsQueryable()); 
    } 

上述方法,没有被命中url http://localhost:port/odata/People(3)总是返回404 Not Found。

我已经配置从暂存具有以下文件的新Asp.Net OData的Web应用程序:

PeopleController.cs

[EnableQuery] 
public class PeopleController : ODataController 
{ 

    // GET /odata/People 
    public IHttpActionResult Get() 
    { 
     return Ok(DemoDataSources.Instance.People.AsQueryable()); 
    } 

    // GET /odata/People(3) 
    public SingleResult<Person> Get([FromODataUri] int key) 
    { 
     return SingleResult.Create(DemoDataSources.Instance.People.Where(p => p.ID == key.ToString()).AsQueryable()); 
    } 
} 

WebApiConfig.cs

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     // Web API configuration and services 
     // Configure Web API to use only bearer token authentication. 
     config.SuppressDefaultHostAuthentication(); 
     config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); 

     // Web API routes 
     config.MapHttpAttributeRoutes(); 

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

     config.MapODataServiceRoute("odata", "odata", GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer)); 
     config.EnsureInitialized(); 
    } 

    private static IEdmModel GetEdmModel() 
    { 
     ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); 
     builder.Namespace = "Demos"; 
     builder.ContainerName = "DefaultContainer"; 
     builder.EntitySet<Person>("People"); 
     builder.EntitySet<Trip>("Trips"); 
     var edmModel = builder.GetEdmModel(); 
     return edmModel; 
    } 
} 

DemoDataSources.cs

public class DemoDataSources 
{ 
    private static DemoDataSources instance = null; 
    public static DemoDataSources Instance 
    { 
     get 
     { 
      if (instance == null) 
      { 
       instance = new DemoDataSources(); 
      } 
      return instance; 
     } 
    } 
    public List<Person> People { get; set; } 
    public List<Trip> Trips { get; set; } 
    private DemoDataSources() 
    { 
     this.Reset(); 
     this.Initialize(); 
    } 
    public void Reset() 
    { 
     this.People = new List<Person>(); 
     this.Trips = new List<Trip>(); 
    } 
    public void Initialize() 
    { 
     this.Trips.AddRange(new List<Trip>() 
     { 
      new Trip() 
      { 
       ID = "0", 
       Name = "Trip 0" 
      }, 
      new Trip() 
      { 
       ID = "1", 
       Name = "Trip 1" 
      }, 
      new Trip() 
      { 
       ID = "2", 
       Name = "Trip 2" 
      }, 
      new Trip() 
      { 
       ID = "3", 
       Name = "Trip 3" 
      } 
     }); 
     this.People.AddRange(new List<Person> 
     { 
      new Person() 
      { 
       ID = "001", 
       Name = "Angel", 
       Trips = new List<Trip>{Trips[0], Trips[1]} 
      }, 
      new Person() 
      { 
       ID = "002", 
       Name = "Clyde", 
       Description = "Contrary to popular belief, Lorem Ipsum is not simply random text.", 
       Trips = new List<Trip>{Trips[2], Trips[3]} 
      }, 
      new Person() 
      { 
       ID = "003", 
       Name = "Elaine", 
       Description = "It has roots in a piece of classical Latin literature from 45 BC, making Lorems over 2000 years old." 
      } 
     }); 
    } 
} 

Person.cs

public class Person 
{ 
    [Key] 
    public String ID { get; set; } 
    [Required] 
    public String Name { get; set; } 
    public String Description { get; set; } 
    public List<Trip> Trips { get; set; } 
} 

Trip.cs

public class Trip 
{ 
    [Key] 
    public String ID { get; set; } 
    [Required] 
    public String Name { get; set; } 
} 

我 “觉得” 这个问题与OData的路由做的,但我不知道为什么这样一个基本的行为不能正常工作...

感谢任何帮助! 马科斯

回答

1

在这种情况下你传递一个字符串值正如Andriy所建议的那样,您可能还需要更改get的签名。

因此改变: 公共SingleResult获取([FromODataUri] INT键)

要: 公共SingleResult获取([FromODataUri]字符串键)

然后,我想你可以调用OData服务为Andriy建议。