简体   繁体   English

OData WebApi:自定义操作不起作用

[英]OData WebApi: Custom action not working

I am using ASP.Net WebApi that is enabled with OData (using ODataController). 我正在使用通过OData(使用ODataController)启用的ASP.Net WebApi。

I have a requirement to accomplish which I need to add a custom action but that not working, it looks I have missed something, can anybody let me know where I am doing wrong? 我需要完成一项工作,我需要添加一个自定义操作,但该工作不起作用,看来我错过了一些东西,有人可以让我知道我在哪里做错了吗?

Thanks in advance!! 提前致谢!!

Below are code snippets of my implementation. 以下是我的实现的代码段。

Custom action 自定义动作

[HttpGet]
[Queryable]
public IQueryable<User> PersonalInfo()
{
    int UserID = this.UserTicket.User_ID;
    return _users.Queryable().Where(d => d.UserID == UserID);
}

Adding action in ODataConfig 在ODataConfig中添加操作

builder.EntitySet<User>(typeof(User).Name);
ActionConfiguration personalInfo = builder.Entity<User>().Collection.Action("PersonalInfo");

Generated Metadata 生成的元数据

<FunctionImport Name="PersonalInfo" IsBindable="true" m:IsAlwaysBindable="true">
    <Parameter Name="bindingParameter" Type="Collection(Models.User)" Nullable="false"/>
</FunctionImport>

EDIT 1 编辑1

I missed to mention, below is the error that I receive from expected url: 我错过了提及,以下是我从预期的URL收到的错误:

No HTTP resource was found that matches the request URI ' http://domain/odata/User/PersonalInfo '. 找不到与请求URI'http:// domain / odata / User / PersonalInfo '匹配的HTTP资源。

An OData Action is always uses the http verb POST and is typically for methods that have an effect on the server, however your controller method has the HttpGet attribute and it looks like you are trying to call this with a GET. OData操作始终使用http谓词POST,通常用于对服务器HttpGet方法,但是您的控制器方法具有HttpGet属性,并且您似乎正在尝试使用GET调用它。 Looking at what the method actually does (just retrieving some data) I think that you want to have an OData Function instead of an Action. 查看该方法的实际作用(只是检索一些数据),我认为您想拥有OData函数而不是Action。 To do this, just change your Action call to be a Function call like this: 为此,只需将您的Action调用更改为Function调用,如下所示:

FunctionConfiguration personalInfo = builder.Entity<User>().Collection.Function("PersonalInfo");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM