简体   繁体   中英

HelpPage for Web Api using OData not working

I'm using the default MVC Help Page for a Web API that uses OData, it works great but if I inherit ODataController instead of ApiController then the Help Page doesn't generate. It shows blank. Only classes that inherit from ApiController show up on the Help Page.

public class BaseApiController : ApiController

works fine and generates in the Help Page

public class BaseApiController : ODataController

doesn't generate a Help Page even though ODataController inherets from ApiController

Any ideas? Thank you

Here is how you do it. It's really easy, but its also dangerous! You will probably NOT want to do this after you see it for yourself.

  1. In your AppStart\\ODataConfig.cs file, make sure the route prefix is set to whatever your prefix is in AppStart\\WebApiConfig.cs. In most cases, it is api.

    example: config.Routes.MapODataRoute("odata", "api", model);

  2. Then add this line to each controller that implements ODataController that you want to show in the help.
    [ApiExplorerSettings(IgnoreApi = false)]


That's it really. The popular HelpPage plugin relies on IApiExplorer which observes the decorator. The OdataController abstract class has this decorator set with (IgnoreApi = true).

If you also use the 'Test Button' plugin, it may not work as you expect if you're new to odata, but you can ignore the default stuff and place a proper odata query in there and execute it.

Now the part that really sucks is that you seem to lose odata navigation if your odata config and regular api config share the same prefix. So given that the odata conventional procedures displayed by the help page are not directly consumable, and in order to preserve navigation the indicated prefix would be wrong, it is not worth leaving this in a production app.

What I would do, is put a link to the meta data in the help page View.

Now if we can get someone at Microsoft to Open Source this or update it for odata controllers, that would be great.

This is expected. HelpPage is intentionally not supported for OData based controllers.

Оf course you can show the controller and the method on the help page just put an attribute:

[ApiExplorerSettings(IgnoreApi = false)]

You can even show what returns your method to this attribute:

[ResponseType(typeof(...))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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