简体   繁体   English

使用OData无法正常工作的Web页面帮助页面

[英]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. 我正在使用默认的MVC帮助页面用于使用OData的Web API,它工作得很好但是如果我继承ODataController而不是ApiController,则不会生成帮助页面。 It shows blank. 它显示为空白。 Only classes that inherit from ApiController show up on the Help Page. 只有继承自ApiController的类才会显示在帮助页面上。

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 即使ODataController来自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. 在AppStart \\ ODataConfig.cs文件中,确保将路由前缀设置为AppStart \\ WebApiConfig.cs中的前缀。 In most cases, it is api. 在大多数情况下,它是api。

    example: config.Routes.MapODataRoute("odata", "api", model); 例如: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. 然后将此行添加到每个实现要在帮助中显示的ODataController的控制器。
    [ApiExplorerSettings(IgnoreApi = false)] [ApiExplorerSettings(IgnoreApi = false)]


That's it really. 真的是这样的。 The popular HelpPage plugin relies on IApiExplorer which observes the decorator. 流行的HelpPage插件依赖于观察装饰器的IApiExplorer。 The OdataController abstract class has this decorator set with (IgnoreApi = true). OdataController抽象类将此装饰器设置为(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. 如果您还使用“测试按钮”插件,如果您不熟悉odata,它可能无法正常工作,但您可以忽略默认内容并在其中放置正确的odata查询并执行它。

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. 现在真正糟糕的部分是,如果你的odata配置和常规api配置共享相同的前缀,你似乎失去了odata导航。 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. 因此,帮助页面显示的odata常规过程不是直接消耗的,并且为了保留导航,指示的前缀是错误的,不值得将其留在生产应用程序中。

What I would do, is put a link to the meta data in the help page View. 我要做的是,在帮助页面View中添加元数据的链接。

Now if we can get someone at Microsoft to Open Source this or update it for odata controllers, that would be great. 现在,如果我们可以让微软的某个人来开源,或者为odata控制器更新它,那就太好了。

This is expected. 这是预料之中的。 HelpPage is intentionally not supported for OData based controllers. 基于OData的控制器有意支持HelpPage。

О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(...))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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