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

后端 未结 3 1026
隐瞒了意图╮
隐瞒了意图╮ 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;
        //        }
    
                /// 
                /// GlobalConfiguration By default
                /// 
                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;
            }
    
            /// 
            /// GlobalConfiguration By default
            /// 
            protected static HttpConfiguration Configuration { get; private set; }
    ....
    

提交回复
热议问题