简体   繁体   English

odata路由无法正常工作

[英]odata routing not working as expected

This is my first OData application with asp.net MVC and i am not able to make it work. 这是我的第一个使用asp.net MVC的OData应用程序,但我无法使其工作。 I need to return a single Summary object from the SummaryController , but facing an issue. 我需要从SummaryController返回单个Summary对象,但面临一个问题。

routing configuration - 路由配置-

public static IEdmModel CreateEdmModel()
{
    ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.EntitySet<Summary>("Summary");
    return modelBuilder.GetEdmModel();
}

public static void Register(HttpConfiguration config)
{
    config.Routes.MapODataRoute("OData", "odata", CreateEdmModel());
    config.EnableQuerySupport();    
    config.EnableSystemDiagnosticsTracing();
}

controller and action method - 控制器和动作方法-

public class SummaryController : ODataController
{
        public Summary Get()
        {
             //....
             return someObj;
        }
}

The route that does not work - 无效的路线-

/odata/Summary

Can anyone please help me understand how can i make the routing work? 谁能帮我了解如何使路由工作?

In the model, you have setup Summary to be an EntitySet, this would return a collection of Summary objects. 在模型中,您将Summary设置为EntitySet,这将返回Summary对象的集合。 If you want that URL to always return one object then you need a Singleton. 如果希望该URL始终返回一个对象,则需要一个Singleton。

The CreateEdmModel method should look like this: CreateEdmModel方法应如下所示:

public static IEdmModel CreateEdmModel()
{
    ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.Singleton<Summary>("Summary");
    return modelBuilder.GetEdmModel();
}

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

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