Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api 来自ODatauri的Web Api OData v4始终返回404未找到_Asp.net Web Api_Odata_Asp.net Web Api Odata - Fatal编程技术网

Asp.net web api 来自ODatauri的Web Api OData v4始终返回404未找到

Asp.net web api 来自ODatauri的Web Api OData v4始终返回404未找到,asp.net-web-api,odata,asp.net-web-api-odata,Asp.net Web Api,Odata,Asp.net Web Api Odata,我正在尝试调用从uri接收参数的web api odata控制器方法,如下所示: // GET /odata/People(3) public SingleResult<Person> Get([FromODataUri] int key) { return SingleResult.Create(DemoDataSources.Instance.People.Where(p => p.ID == key.ToString()).AsQu

我正在尝试调用从uri接收参数的web api odata控制器方法,如下所示:

    // GET /odata/People(3)
    public SingleResult<Person> Get([FromODataUri] int key)
    {
        return SingleResult.Create(DemoDataSources.Instance.People.Where(p => p.ID == key.ToString()).AsQueryable());
    }
我“认为”这个问题与odata路由有关,但我不知道为什么这样的基本行为不能正常工作

谢谢你的帮助!
Marcos

您需要对路由中的id使用单引号,因为
Person
类('3')的键属性的
string
类型
您需要对路由中的id使用单引号,因为
Person
类('3')的键属性的
string
类型

如果按照Andrey的建议传递字符串值,则可能还需要更改get的签名

因此,改变: 公共单结果获取([FromODataUri]int-key)

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


然后我认为您可以按照Andrey的建议调用OData服务。

如果您按照Andrey的建议传递字符串值,您可能还需要更改get的签名

因此,改变: 公共单结果获取([FromODataUri]int-key)

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

然后我想你可以按照安德烈的建议打电话给OData服务

[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());
    }
}
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;
    }
}
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."
            }
        });
    }
}
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; }
}
public class Trip
{
    [Key]
    public String ID { get; set; }
    [Required]
    public String Name { get; set; }
}