【问题标题】:ASP.Net Web API Help Page Area returning empty outputASP.Net Web API 帮助页面区域返回空输出
【发布时间】:2017-08-15 22:55:06
【问题描述】:

我有一个预先存在的 MVC 应用程序,我使用 Nuget 添加了 Web API 和 Web API 自我文档。虽然 Web API 控制器功能正常(返回对 HTTP 请求的有效响应),但帮助控制器没有找到任何要记录的 Web API 方法。

在帮助控制器索引操作中“Configuration.Services.GetApiExplorer().ApiDescriptions”返回 0 个结果。

什么填充了 ApiDescriptions,我需要设置任何配置设置以将我的 api 公开给文档吗?

帮助区域与我的应用程序的其余部分是分开的。这是否会导致找到控制器的部分找不到我的控制器?此外,我什至在 HelpController 本身中添加了一个帮助片段,但仍然没有 API 描述。

我的 API 控制器也有特殊的路由,所以我不确定这是否相关。

【问题讨论】:

    标签: asp.net-web-api documentation documentation-generation


    【解决方案1】:

    经过一番搜索,我找到了this post,它也指的是this post

    正如第一篇文章中提到的,Glimpse 是罪魁祸首,这个解决方法为我解决了这个问题:

    <glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
    <inspectors>
       <ignoredTypes>
          <add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet"/>
       </ignoredTypes>
    </inspectors>
    </glimpse>
    

    这也是一个已知问题,解决方法在此Glimpse GitHub Issue 中进行了描述。

    【讨论】:

    • 感谢您的回复,但我不认为我在使用 glimpse!
    • 谢谢!解决了我的问题。
    【解决方案2】:

    我有同样的问题,我不使用 Glimpse,我这样解决问题:

    ProjectName\Areas\HelpPage\Controllers\HelpController.cs 文件中注释构造函数,因为不称为隐式构造函数public HelpController() : this(GlobalConfiguration.Configuration) 默认称为带有参数public HelpController(HttpConfiguration config) 的构造函数,并且Configuration 属性的这种初始化是不正确的。你可以这样解决这个问题:

    解决方案 1: 注释/删除构造函数。

    public class HelpController : Controller
            {
                private const string ErrorViewName = "Error";
    
        //        public HelpController()
        //            : this(GlobalConfiguration.Configuration)
        //        {
        //        }
    
        //        public HelpController(HttpConfiguration config)
        //        {
        //            Configuration = config;
        //        }
    
                /// <summary>
                /// GlobalConfiguration By default
                /// </summary>
                protected static HttpConfiguration Configuration
                {
                    get { return GlobalConfiguration.Configuration; }
                }
    
                public ActionResult Index()
                {
                    ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
                    return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
                }
    ....
    

    解决方案 2: 通过添加此属性 [InjectionConstructor] 来注入默认构造函数。

    public class HelpController : Controller
        {
            private const string ErrorViewName = "Error";
    
            [InjectionConstructor]
            public HelpController()
                : this(GlobalConfiguration.Configuration)
            {
            }
    
            public HelpController(HttpConfiguration config)
            {
                Configuration = config;
            }
    
            /// <summary>
            /// GlobalConfiguration By default
            /// </summary>
            protected static HttpConfiguration Configuration { get; private set; }
    ....
    

    问题解决了。

    【讨论】:

    • InjectionConstructor 构造函数从何而来?团结?
    • @ChrisMcKelt 是来自 Unity 的 Chris
    • 解决方案 1 对我不起作用,因为它导致了空引用异常。只要您添加对 Microsoft.Practices.Unity 的引用,解决方案 2 就可以正常工作。显然取决于您的项目,这可能会或可能不会接受,但在我的情况下,非常感谢您的帮助!
    • 解决方案2:在配置Unity时使用工厂container.RegisterFactory&lt;HttpConfiguration&gt;(c =&gt; GlobalConfiguration.Configuration);代替注入属性,这样就消除了对Unity的依赖
    【解决方案3】:

    我可以通过在我的 Application_Start () 方法中添加 GlobalConfiguration.Configure (WebApiConfig.Register); 来解决这个问题。因为我的应用程序使用 OWIN,所以我只在 Startup.Configuration (IAppBuilder app) 中注册我的 API。

    【讨论】:

      【解决方案4】:

      NuGet package manager 安装 HelpPages 包后 - 导航到 WebApplication1\Areas\HelpPage\App_Start\HelpPageConfig.cs 并取消注释下面的行

       config.SetDocumentationProvider(new XmlDocumentationProvider(
          HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
      

      还将App_Data/XmlDocument.xml 添加到 WebApplication > Properties > Build > Check XML Documentation File

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-08
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-27
        相关资源
        最近更新 更多