【问题标题】:ASP.NET Web API OData V4 Service Returning HTTP 404 ($top)ASP.NET Web API OData V4 服务返回 HTTP 404 ($top)
【发布时间】:2018-12-05 00:26:37
【问题描述】:

我正在尝试使用 C#(VS 2017,.Net 4.6.1)构建 OData 服务,但无法使 OData 查询字符串选项正常工作。返回组或单个 MusicItem 的调用有效,但是当我添加 $top=2 时,我收到 HTTP 404 响应。我已经在网上研究了两天,但找不到答案。

想法?

WebApiConfig如下:

using md.music.services.Models;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.OData.Edm;
using System.Web.Http;

namespace md.music.services
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.MapHttpAttributeRoutes();

            config.MapODataServiceRoute("ODataRoute", "odata", GetImplicitEDM());

        }

        public static IEdmModel GetImplicitEDM()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet<MusicItem>("MusicItems");

            return builder.GetEdmModel();

        }

    }

}

MusicItemsController 如下:

using md.music.services.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Query;
using System.Linq;
using System.Web.Http;

namespace md.music.services.Controllers
{
    public class MusicItemsController : ODataController
    {
        WA0002Entities entities = new WA0002Entities();

        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
        public IQueryable<MusicItem> Get(ODataQueryOptions opts)
        {
            return entities.MusicItems.AsQueryable<);

        }

        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
        public MusicItem Get([FromODataUri] int key)
        {
            MusicItem thisObject = entities.MusicItems.FirstOrDefault(c => c.RowID == key);

            if (thisObject == null)
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);

            }

            return thisObject;

        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            entities.Dispose();

        }

    }

}

【问题讨论】:

    标签: c# asp.net odata


    【解决方案1】:

    您收到 404 错误的事实可能是由于您没有在 url 中添加 $top 作为 查询参数。您需要在 ?

    之后添加所有 odata 查询

    网址应该类似于:http://example.com?$top=2

    另外,我认为您需要为用户可以查询的最大 top 指定一个值,否则默认为 0,您将收到如下错误消息:

    "URI 中指定的查询无效。'0' 的限制为 已超出顶部查询。传入请求的值为 '2'。",

    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All, MaxTop = 10)]
    

    更新:

    您还需要通过执行以下操作在实体中启用分页:

    public static IEdmModel GetImplicitEDM()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    
            builder
                .EntitySet<MusicItem>("MusicItems")
                .Page();
    
            return builder.GetEdmModel();
    
        }
    

    【讨论】:

    • 谢谢,但是当我添加“?”我收到一个 HTTP 400;否则,仍然返回 HTTP 404。
    • 你肯定需要使用“?”使用 odata 查询时。现在你收到了一个错误的请求。我猜您收到以下消息:“URI 中指定的查询无效。已超出 Top 查询的 '0' 限制。传入请求的值为 '10'。”您需要在实体中启用分页:builder.EntitySet("MusicItems").Page();
    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多