我有一个应用程序,需要动态添加 OData 路由。我可以在 Application_Start 之后添加常规路由,但使用OData路由时遇到困难。
以下是我尝试动态添加 OData Web Api 路由的方法。在我的 WebApiConfig 中,我添加了一个产品路由:
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());
}
}
然后在我的 ProductsController 中,我在 Products GET 方法中添加以下调用,当我转到 http:///odata/Products 时成功调用该调用(是的,有点奇怪,但这是演示添加Application_Start 之后的路线):
GlobalConfiguration.Configure(WebApiConfig.AddOrderRoute);
WebApiConfig.AddOrderRoute 方法被正确调用并执行,没有错误:
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/Orders 时,出现以下错误:
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)
请注意,我正在 AddOrderRoute 中调用 config.EnsureInitialized()
。
我错过了什么?
注意:如果我在 WebApiConfig.Register() 方法末尾调用 AddOrderRoute,则我的订单路由可用且正在工作,因此我知道我的订单实体、上下文和 Controller 工作正常。只有当我在应用程序初始化完成后调用它时,我才会遇到问题。
最佳答案
嗯,我花了一些时间与 MSDN 上的好心人交流并得到了答案!
以下是需要注册 Orders 实体路由的方式:
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/Orders 可以正常访问了。
关于dynamic - 如何在*Application_Start之后动态添加OData Web Api路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25293477/