如何使用oData在MVC中创建一个动作

时间:2013-11-13 23:05:15

标签: c# asp.net-mvc odata

我试图按照

中的示例代码进行操作

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请求。

1 个答案:

答案 0 :(得分: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中实现。