如何动态地添加的OData网页API路线*的Application_Start之后? [英] How to dynamically add OData Web Api routes *after* Application_Start?

查看:366
本文介绍了如何动态地添加的OData网页API路线*的Application_Start之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我需要动态地添加OData的路线。我可以添加的常规的的Application_Start之后的路线就好了,但我有麻烦的的OData 的路线做。

I have an application where I need to add OData routes dynamically. I can add regular routes after Application_Start just fine, but am having trouble doing it with OData routes.

下面就是我正在尝试动态添加的OData网页API路线。在我WebApiConfig,我添加了一个产品路线:

Here's how I'm trying to dynamically add OData Web Api routes. In my WebApiConfig, I add a Products route:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Products");
        config.MapODataServiceRoute(routeName: "ProductsRoute", routePrefix: "odata", model: builder.GetEdmModel());
    }
}

当我去到http

然后在我的ProductsController,我添加以下调用在产品GET方法,它是所谓的成功:/// ODATA /产品(是的,有点怪异,但它证明添加的一种方式之后的Application_Start路线):

Then in my ProductsController, I add the following call in the Products GET method, which is successfully called when I go to http:///odata/Products (yeah, a little weird, but it's one way to demonstrate adding a route after Application_Start):

GlobalConfiguration.Configure(WebApiConfig.AddOrderRoute);

该WebApiConfig.AddOrderRoute方法被称为正确无误的执行:

The WebApiConfig.AddOrderRoute method gets called correctly and executes without error:

public static void AddOrderRoute(HttpConfiguration config)
{
    var builder = new ODataConventionModelBuilder();
    builder.EntitySet<Order>("Orders");
    config.MapODataServiceRoute(routeName: "OrdersRoute", routePrefix: "odata", model: builder.GetEdmModel());
    config.EnsureInitialized();
}

所以,你会想,现在我有一个订单路由配置正确。但是,当我去到http:/// ODATA /订单,我得到以下错误:

So you'd think that now I have an Orders route correctly configured. But when I go to http:///odata/Orders, I get the following error:

ExceptionMessage=The object has not yet been initialized. 
Ensure that HttpConfiguration.EnsureInitialized() is called in the 
application's startup code after all other initialization code.

StackTrace=   at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.get_AttributeMappings()
   at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.SelectController(ODataPath odataPath, HttpRequestMessage request)
   at System.Web.OData.Routing.ODataPathRouteConstraint.SelectControllerName(ODataPath path, HttpRequestMessage request)
   at System.Web.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection)
   at System.Web.Http.Routing.HttpRoute.ProcessConstraint(HttpRequestMessage request, Object constraint, String parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection)
   at System.Web.Http.Routing.HttpRoute.ProcessConstraints(HttpRequestMessage request, HttpRouteValueDictionary values, HttpRouteDirection routeDirection)
   at System.Web.Http.Routing.HttpRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)
   at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)

请注意,我打电话 config.EnsureInitialized()在AddOrderRoute。

Note that I'm calling config.EnsureInitialized() in AddOrderRoute.

我在想什么?

注意:如果我打电话AddOrderRoute在WebApiConfig.Register的end()方法,我的订单路由可用和工作,所以我知道我的Order实体,环境和控制器正常工作。只有当我把它称为一次应用程序初始化完成后,我得到这个问题。

NOTE: If I call AddOrderRoute at the end of WebApiConfig.Register() method, my Orders route is available and working, so I know that my Order entity, context and controller are working fine. It's only when I call it once application initialization is done that I get the problem.

推荐答案

好了,我花了一些时间与MSDN上的好乡亲,并得到一个答案!

Well, I spent some time with the good folks over at MSDN and got an answer!

这里的Orders实体路线需要注册方式:

Here's the way the Orders entity route needs to be registered:

var builder = new ODataConventionModelBuilder();
builder.EntitySet<Order>("Orders");

var route = config.Routes.Where(r => r is System.Web.OData.Routing.ODataRoute).First();
var odataRoute = route as System.Web.OData.Routing.ODataRoute;

config.MapODataServiceRoute(
    routeName: "OrdersRoute", 
    routePrefix: "odata", 
    model: builder.GetEdmModel(),
    pathHandler: odataRoute.PathRouteConstraint.PathHandler,
    routingConventions: odataRoute.PathRouteConstraint.RoutingConventions);

尼斯。现在... / ODATA /订单可以访问没有问题的。

Nice. Now .../odata/Orders can be accessed no problem.

这篇关于如何动态地添加的OData网页API路线*的Application_Start之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆