Asp.net web api ASP.NETWebAPI帮助页区域返回空输出
我有一个先前存在的MVC应用程序,我使用Nuget添加了Web API和Web API自我文档。虽然Web API控制器工作正常(返回对HTTP请求的有效响应),但帮助控制器找不到任何要记录的Web API方法 在帮助控制器索引操作中,“Configuration.Services.GetApiExplorer().apisdescriptions”返回0个结果 要将api公开给文档,我需要设置哪些配置描述和设置 帮助区域与我的应用程序的其余部分是一个单独的区域。这是否导致找到控制器的工件找不到我的控制器?此外,我甚至在HelpController本身中添加了一个被截断的帮助,这仍然导致没有API描述Asp.net web api ASP.NETWebAPI帮助页区域返回空输出,asp.net-web-api,documentation,documentation-generation,Asp.net Web Api,Documentation,Documentation Generation,我有一个先前存在的MVC应用程序,我使用Nuget添加了Web API和Web API自我文档。虽然Web API控制器工作正常(返回对HTTP请求的有效响应),但帮助控制器找不到任何要记录的Web API方法 在帮助控制器索引操作中,“Configuration.Services.GetApiExplorer().apisdescriptions”返回0个结果 要将api公开给文档,我需要设置哪些配置描述和设置 帮助区域与我的应用程序的其余部分是一个单独的区域。这是否导致找到控制器的工件找不到
我的API控制器也有特殊的路由,所以我不确定这是否相关。在进一步搜索后,我发现它也引用了 正如在第一篇文章中提到的,“一瞥”是罪魁祸首,这个解决方法为我解决了这个问题:
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<inspectors>
<ignoredTypes>
<add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet"/>
</ignoredTypes>
</inspectors>
</glimpse>
.我也有同样的问题,我不使用“一瞥”,而是这样解决问题:
在ProjectName\Areas\HelpPage\Controllers\HelpController.cs
文件中,注释构造函数,因为它不是隐式构造函数public HelpController():这个(GlobalConfiguration.Configuration)
default被称为带有参数public HelpController(HttpConfiguration)的构造函数
而配置
属性的初始化是不正确的。你可以这样解决这个问题:
解决方案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);
}
....
公共类帮助控制器:控制器
{
私有常量字符串ErrorViewName=“Error”;
//公共助理总监()
//:此(GlobalConfiguration.Configuration)
// {
// }
//公共帮助控制器(HttpConfiguration配置)
// {
//配置=配置;
// }
///
///默认情况下的全局配置
///
受保护的静态http配置
{
获取{return GlobalConfiguration.Configuration;}
}
公共行动结果索引()
{
ViewBag.DocumentationProvider=配置.Services.GetDocumentationProvider();
返回视图(Configuration.Services.GetApiExplorer().apisdescriptions);
}
....
解决方案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; }
....
公共类帮助控制器:控制器
{
私有常量字符串ErrorViewName=“Error”;
[注入构造函数]
公共助理总监()
:此(GlobalConfiguration.Configuration)
{
}
公共帮助控制器(HttpConfiguration配置)
{
配置=配置;
}
///
///默认情况下的全局配置
///
受保护的静态HttpConfiguration配置{get;private set;}
....
问题解决了。我可以通过在我的应用程序启动()方法中添加GlobalConfiguration.Configure(WebApiConfig.Register);
来解决这个问题。因为我的应用程序使用OWIN,所以我只在Startup.Configuration(IAppBuilder应用程序)中注册API
从NuGet package manager
安装HelpPages软件包后-导航到WebApplication1\Areas\HelpPage\App\u Start\HelpPageConfig.cs
并取消注释下面的行
config.SetDocumentationProvider(new XmlDocumentationProvider(
HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
还要将App_Data/XmlDocument.xml
添加到WebApplication>Properties>Build>Check xml文档文件感谢您的回复,但我不认为我使用的是Skip!谢谢!修复了我的问题。InjectionConstructor构造函数来自哪里?Unity?@ChrisMcKelt是的UnitySolution 1的Chris对我不起作用,因为它是ca使用空引用异常。解决方案2可以正常工作,只要您添加对Microsoft.Practices.Unity的引用。显然,根据您的项目,这可能是可以接受的,也可能是不可以接受的,但在我的情况下,非常感谢您的帮助!对于解决方案2:在配置Unitycontainer.RegisterFact时使用工厂,而不是注入属性(c=>GlobalConfiguration.Configuration);
,这消除了对Unity的依赖