ASP.Net Web Api - ApiExplorer does not contain any ApiDescriptions

后端 未结 3 1023
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 08:54

I am trying to implement an Options method in a controller of my web service that will return a message containing the valid HTTP methods for the URI endpoint associated wit

相关标签:
3条回答
  • 2021-01-04 09:18

    The solution for this problem is to comment in ProjectName\Areas\HelpPage\Controllers\HelpController.cs the constructors like this:

        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);
                }
    ....
    

    The default constructor is not called;

    The second method is to inject the default constructor by add this attribute [InjectionConstructor] on default constructor like this:

    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; }
    ....
    
    0 讨论(0)
  • 2021-01-04 09:36

    If you use Glimpse, you might have to disable it's route inspector:

    <glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
      <logging level="Off" />
      <tabs>
        <ignoredTypes>
          <add type="Glimpse.AspNet.Tab.Routes, Glimpse.AspNet" />
        </ignoredTypes>
      </tabs>
      <inspectors>
        <ignoredTypes>
          <add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet" />
        </ignoredTypes>
      </inspectors>
    </glimpse>
    

    Glimpse creates RouteProxies that break enumeration in HostedHttpRouteCollection: https://github.com/mono/aspnetwebstack/blob/master/src/System.Web.Http.WebHost/Routing/HostedHttpRouteCollection.cs

    I know the link is for mono but the same is true for standard .Net.

    0 讨论(0)
  • 2021-01-04 09:37

    You should look to upgrade to the RTM of WebApi that was released yesterday and then check out the newly released ASP.NET WebApi Help Page (Preview) that was also released yesterday.

    This package automatically generates help page content for Web APIs on your site. Visitors to your help page can use this content to learn how to call your web APIs. Everything generated by the help page is fully customizable using ASP.NET MVC and Razor.

    It is implementing the ApiExplorer under the covers.

    0 讨论(0)
提交回复
热议问题