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

启动时出现OData v4错误:找不到该段'无论'的资源

我正在构建我的新v4服务,一切顺利,直到我为新模型/实体添加了一个新控制器,并在启动网站进行测试运行时遇到此错误.

控制器似乎正确编码,就像其他控制器一样.

控制器"CustomersOData"中操作"GetFeed"上的路径模板"Customers"不是有效的OData路径模板.未找到"客户"细分的资源.

这对地球意味着什么?

c# odata asp.net-web-api asp.net-web-api-odata

8
推荐指数
1
解决办法
8462
查看次数

无法使用oData查询选项

我有一个ASP.NET Web API项目.我试图将一些查询选项传递给我的API控制器,如下所示:

http://localhost:61736/api/Enquiries?
callback=callback&$top=30&$skip=30&orderby=EnquiryId
&$inlinecount=allpages&_=1346164698393
Run Code Online (Sandbox Code Playgroud)

但我得到以下内容:

The query parameter '$inlinecount' is not supported.

当我尝试使用时$callback,我也会得到相同的$format

知道我做错了什么吗?根据:http://msdn.microsoft.com/en-us/library/ff478141.aspx我应该可以使用它们吗?

c# odata asp.net-web-api asp.net-web-api-odata

7
推荐指数
1
解决办法
9259
查看次数

如何在自托管Web API应用程序中配置OData端点

我正在构建一个OWIN自托管Web API 2服务.我需要此服务来公开OData端点.

传统的IIS托管方法涉及App_Start/WebApiConfig.cs:

using ProductService.Models;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // New code:
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Products");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: builder.GetEdmModel());
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,在我的自托管解决方案中,没有WebApiConfig.cs这样的东西

我在哪里以及如何指定此OData配置?

c# self-hosting asp.net-web-api owin asp.net-web-api-odata

7
推荐指数
1
解决办法
4142
查看次数

使用$ expand选项返回导航属性时,Web Api OData v4请求失败

对OData服务的请求类似于下面的请求按预期工作:

http:// localhost:31054/odata/Articles?$ filter = ArticleRatings/any()&$ orderby = ArticleId%20desc&$ expand = Publication&$ format = json

但是,当我将格式更改为xml时,相同的请求将失败,并显示以下错误消息:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
        The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
    </ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace/>
    <InnerException>
        <Message>An error has occurred.</Message>
        <ExceptionMessage>
            Object of type 'System.Linq.EnumerableQuery`1[System.Web.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand`1[DataAccess.OData.Article]]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[DataAccess.OData.Article]'.
        </ExceptionMessage>
        <ExceptionType>System.ArgumentException</ExceptionType>
        <StackTrace>
            at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) at System.Reflection.MethodBase.CheckArguments(Object[] parameters, …
Run Code Online (Sandbox Code Playgroud)

asp.net-web-api-odata odata-v4

7
推荐指数
0
解决办法
229
查看次数

过滤器收集的OData uri为空或任何eq 4

使用OData,我该如何制作这个过滤器?

我的课:

public class Aviso
{
    public int Id { get; set; }
    public virtual ICollection<User> Destinatarios { get; set; }
    public string Url { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

uri尝试: /odata/avisos?$filter=(Destinatarios eq null or Destinatarios/count eq 0 or Destinatarios/any(it:it/Id eq 4) )

目标是返回任何Destinatarios4或集合为空(或null)的位置.

c# odata asp.net-web-api asp.net-web-api-odata

6
推荐指数
1
解决办法
4978
查看次数

以XML格式获取Web API OData v4的OData目录

我正在尝试启动并运行Web API OData V4端点.

我终于得到了它(在从我的端点删除所有DateTime属性之后),现在实体的列表是JSON.

我喜欢JSON,但我使用LinqPad来测试我的端点.它不理解我的Feed中实体列表的JSON.

我看起来似乎无法找到一种方法将其更改回XML,所以我在这里问.

有没有办法让Web API OData v4提要的实体列表是XML而不是JSON?

.net odata asp.net-web-api-odata odata-v4

6
推荐指数
1
解决办法
5508
查看次数

OData v4自定义功能

我正在尝试在OData v4 Web API解决方案中创建自定义函数.我需要返回基于OData本身无法处理的独特逻辑的"Orders"集合.我似乎无法弄清楚如何在不破坏整个OData服务层的情况下创建这个自定义函数.当我使用ODataRoute属性修饰Controller方法时,它都会变成地狱.任何基本请求都会产生相同的错误.有人可以看看下面的代码,看看你是否注意到我必须遗漏的东西?

WebApiConfig.cs

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("odata", "odata", model: GetModel());
    }

    public static Microsoft.OData.Edm.IEdmModel GetModel()
    {
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Account>("Accounts");
        builder.EntitySet<Email>("Emails");
        builder.EntitySet<PhoneNumber>("PhoneNumbers");
        builder.EntitySet<Account>("Accounts");
        builder.EntitySet<Address>("Addresses");
        builder.EntitySet<Order>("Orders");
        builder.EntitySet<OrderDetail>("OrderDetails");

        var orders = builder.EntityType<Order>();
        var function = orders.Function("GetByExternalKey");
        function.Parameter<long>("key");
        function.ReturnsCollectionFromEntitySet<Order>("Orders");

        return builder.GetEdmModel();
     }
 }
Run Code Online (Sandbox Code Playgroud)

OrdersController.cs

public class OrdersController : ODataController
{
    private SomeContext db = new SomeContext();

    ...Other …
Run Code Online (Sandbox Code Playgroud)

c# odata asp.net-web-api asp.net-web-api-odata

6
推荐指数
1
解决办法
9949
查看次数

应用Distinct到OData查询

我想从OData端点获取不同值的列表.但不支持distinct或group by.

我的URI查询看起来像这样

GET /odata/Products?$select=foo & $top=10 & $count=true & distinct=true
Run Code Online (Sandbox Code Playgroud)

我的控制器

[EnableQuery]
public IQueryable<FooBarBaz> Get(ODataQueryOptions<FooBarBaz> queryOptions, bool distinct)
{
        //I've tried the following
        return Repository.AsQueryable().Distinct();

        // and
        return Repository.AsQueryable().GroupBy(x => x.Foo);

        // and
        IQueryable query = queryOptions.ApplyTo(Repository.AsQueryable());
        return query.Distinct(); // Can't call .Distinct() here
}
Run Code Online (Sandbox Code Playgroud)

没有工作:(

c# odata asp.net-web-api-odata odata-v4

6
推荐指数
2
解决办法
1万
查看次数

使用OData $ select来从相关对象中挑选字段

我正在使用带有OData V4的WebAPI 2.2.

我可以使用$filter=RelatedObj/PropertyName eq 'Some Value'基于相关对象属性值来过滤实体列表.

但是,当我尝试使用相同的语法时$select:

$select=Id,Name,RelatedObj/PropertyName
Run Code Online (Sandbox Code Playgroud)

结果例外:

"message": "The query specified in the URI is not valid. Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties.",
"innererror": {
"message": "Found a path with multiple navigation properties or a bad complex property path in a select clause. Please …
Run Code Online (Sandbox Code Playgroud)

.net odata asp.net-web-api asp.net-web-api-odata

6
推荐指数
1
解决办法
3289
查看次数

WebApi OData每个用户的属性安全性

我有一些实体,其数据必须只能供某些用户访问.

public class Foo
{
    public virtual Bar { get; set; }
    ...
}

public class Bar
{
    public string Secret { get; set; }
    ...
}
Run Code Online (Sandbox Code Playgroud)

例如,Bar.Secret只能通过UserA但不能访问UserB.我可以这样:

public class BarsController : ODataController
{
    [EnableQuery]
    public IHttpActionResult Get()
    {
        if (User.Identity.Name != "UserA") 
            return Unauthorized();

        return _db.Bars();
    }
}
Run Code Online (Sandbox Code Playgroud)

除此之外是一个糟糕的实施.它不包括此控制器:

public class FoosController : ODataController
{
    [EnableQuery]
    public IHttpActionResult Get()
    {
        return _db.Foos();
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个可以调用,/odata/Foos?$expand=Bars然后我可以查看Bar.Secret.我不能只是禁用$expand,Foo因为该查询完全合法 …

c# odata asp.net-web-api asp.net-web-api-odata

6
推荐指数
1
解决办法
308
查看次数