Expose only a subset of .NET OData APIs for a route (return 404 for excluded APIs)

℡╲_俬逩灬. 提交于 2019-12-04 03:07:53

You need two different models or a more intelligent model with conditions as entitiy products is not available in all paths but only in /api/products.
In general the Error-message is explaining it already quite well but perhaps you just need it in other words.

I think the cleaner way is to provide an own model for each route, then it's quite easy to add or remove whatever you need inside.
If you mix everything in one model it will always be under construction if a new route is added or changed.

// Map route 1 to {model}
config.MapODataServiceRoute(
    routeName: "route1",
    routePrefix: "/api",
    model: GetApiModel(), 
    pathHandler: new CustomBIODataPathHandler(), 
    routingConventions: conventions);


// Map route 2 to {model}
config.MapODataServiceRoute(
    routeName: "route2",
    routePrefix: "/api/partial", // different route prefix
    model: GetApiPartialModel(),
    pathHandler: new CustomBIODataPathHandler(), 
    routingConventions: conventions);

It's a concept and I'm not so firm with the notation in that code so you might have to adjust it a bit.

Alternatively, if you really want to use only one model just try it like this:

// Map route 1 to {model}
config.MapODataServiceRoute(
    routeName: "route1",
    routePrefix: "/api",
    model: GetFullEdmModel("/api"), 
    pathHandler: new CustomBIODataPathHandler(), 
    routingConventions: conventions);

// Map route 2 to {model}
config.MapODataServiceRoute(
    routeName: "route2",
    routePrefix: "/api/partial", // different route prefix
    model: GetFullEdmModel("/api/partial"), // but it uses the same model
    pathHandler: new CustomBIODataPathHandler(), 
    routingConventions: conventions);

Then you can use the parameter to implement any conditions or a switch.

Beside that you've probably faults in your desired code in the bottom:

// Map route 1 to {model}
var model = GetFullEdmModel();
var modelConventions = ODataRoutingConventions.CreateDefaultWithAttributeRouting(config, model);
config.MapODataServiceRoute(
    routeName: "route1",
    routePrefix: "/api",
    model: model, // use standard full model
    pathHandler: new CustomBIODataPathHandler(), 
    routingConventions: modelConventions);

See the last line, that has to be adjusted for both blocks then. I never took care before about the two lines above each config-block, so primary the problem is probably that not all variables are fitting together and you've to check it in all details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!