I am building a sample from two Web API OData samples, each of them works fine as a separate project. But when I add second ODataController class, then the site no longer works complaining about OData path templates that worked previously. Here are more details:
The following action works fine as long as its controller (ProductsController) is the only controller:
[HttpGet]
[ODataRoute("GetSalesTaxRate(state={state})")]
public IHttpActionResult GetSalesTaxRate([FromODataUri] string state)
{
return Ok(GetRate(state));
}
Now I add a new controller (MoviesController) with a few actions.
I extend Owin Startup class so it looks like this:
public void Configuration(IAppBuilder builder)
{
var config = new HttpConfiguration();
config.MapODataServiceRoute(routeName: "functions route", routePrefix: "functions", model: FunctionStartup.GetEdmModel());
config.MapODataServiceRoute(routeName: "actions route", routePrefix: "actions", model: ActionStartup.GetEdmModel());
builder.UseWebApi(config);
}
However, when I try to execute a Web request (URLBASE/functions/$metadata), I get the following error:
System.InvalidOperationExceptionThe path template
'GetSalesTaxRate(state={state})' on the action 'GetSalesTaxRate' in
controller 'Products' is not a valid OData path template. Resource not
found for the segment 'GetSalesTaxRate'.
Controllers are mapped to different routes ("functions" and "actions"). Can be that the problem is that each route is mapped to its own EdmModel?
UPDATE. I checked that I can add more controllers as long as they refer to the same EDM model. But once I introduce a second model (and reference it from MapODataServiceRoute), then the whole service breaks. Is there any workaround to support multiple models?
UPDATE 2. If I subclass DefaultHttpControllerTypeResolver and only enable single controller (any of them), then is also works fine. But I am still puzzled why multiple controllers using different models fail.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…