0

我试图按照示例代码

https://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/ODataActionsSample/ODataActionsSample/NonBindableActionRoutingConvention.cs

尽我所能。

WebApiConfig:

        config.Routes.MapODataRoute(
                "AppAccountOData",
                "odata",
                GetModel()
            );

...

API 路由
...

        public static IEdmModel GetModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<User>("Users");
        builder.EntitySet<EventLog>("EventLogs");
        builder.EntitySet<Server>("Servers");
        builder.EntitySet<Database>("Databass");
        builder.EntitySet<Command>("Commands");
        builder.EntitySet<Connection>("Conenctions");
        builder.EntitySet<Query>("Queries");
        builder.EntitySet<DirectoryGroup>("DirectoryGroups");
        builder.EntitySet<ServiceAccount>("ServiceAccounts");
        builder.EntitySet<EmailLog>("EmailLogs");
        builder.EntitySet<EmailTemplate>("EmailTemplates");
        builder.EntitySet<EventLog>("EventLogs");
        builder.EntitySet<EventLogLevel>("EventLogLevels");
        builder.EntitySet<EventLogSource>("EventLogSources");
        builder.EntitySet<ApplicationServer>("ApplicationServers");
        builder.EntitySet<ApplicationOnboardings_Old>("ApplicationOnboardings_Olds");
        builder.EntitySet<ApplicationContact>("ApplicationContacts");
        builder.EntitySet<ApplicationCategory>("ApplicationCategories");
        builder.EntitySet<ApplicationOnboardingStatusType>("ApplicationOnboardingStatusTypes");
        builder.EntitySet<ApplicationOnboarding>("ApplicationOnboardings");
        builder.EntitySet<Application>("Applications");
        builder.EntitySet<AppAccount>("AppAccounts");
        builder.EntitySet<vAppAccountUser>("vAppAccountUsers");
        builder.EntitySet<vUnmatchedAppAccount>("vUnmatchedAppAccounts");
        builder.EntitySet<vTerminatedUser>("vTerminatedUsers");
        builder.EntitySet<SvcAccount>("SvcAccounts");
        builder.EntitySet<AppAccountClass>("AppAccountClasses");

        var serviceAccounts = builder.Entity<AppAccount>().Collection.Action("Service");
        serviceAccounts.ReturnsCollectionFromEntitySet<AppAccount>("AppAccounts");

        return builder.GetEdmModel();

    }

控制器中的功能很简单:

    [HttpGet]
    public ICollection<AppAccount> Service(ODataQueryOptions opts)
    {
        var results = opts.ApplyTo(db.AppAccounts.Where(aa => aa.AppAccountClass.Name == "Service")) as IQueryable<AppAccount>;
        if (results != null) return results.ToList();
        throw new HttpResponseException(HttpStatusCode.BadRequest);
    }

当我调用它时,我收到一个错误:

未实现。此服务不支持“~/entityset/action”形式的 OData 请求。

4

1 回答 1

2

OData 操作调用是 HTTP POST。所以,你的控制器代码应该是这样的,

[HttpPost]
public ICollection<AppAccount> Service(ODataQueryOptions opts)
{
    var results = opts.ApplyTo(db.AppAccounts.Where(aa => aa.AppAccountClass.Name == "Service")) as IQueryable<AppAccount>;
    if (results != null) return results.ToList();
    throw new HttpResponseException(HttpStatusCode.BadRequest);
}

查看您的代码,我认为您需要 OData 功能支持,该功能尚未在 Web API 中实现。

于 2013-11-13T23:56:22.723 回答