我有这个项目,它只是一个 Web API 项目。在过去的某个时候,我删除了 HelpPages,并让应用程序使用了 OWIN。现在我被要求添加我已经完成的 API HelpPages。我已将我的Startup类设置为如下所示:
public void Configuration(IAppBuilder app)
{
// Needs to be first
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
var httpConfig = new HttpConfiguration();
// Register all areas
AreaRegistration.RegisterAllAreas();
ConfigureOAuthTokenGeneration(app);
ConfigureOAuthTokenConsumption(app);
ConfigureWebApi(httpConfig);
app.UseWebApi(httpConfig);
}
这样我的帮助页面路线就可以了。据我所知,这应该可以工作,但问题是 ApiExplorer 不会撤回任何描述。
在我的ConfigureWebApi方法中,我删除了格式,我已将其注释掉,但它仍然不起作用,这是方法:
private void ConfigureWebApi(HttpConfiguration config)
{
// Web API configuration and services
var formatters = config.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var serializerSettings = jsonFormatter.SerializerSettings;
// Remove XML formatting
formatters.Remove(config.Formatters.XmlFormatter);
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
jsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
// Configure our JSON output
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
serializerSettings.Formatting = Formatting.Indented;
serializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
serializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我实际上编辑了HelpController并在返回视图行上放置了一个断点,这就是我知道 ApiExplorer 没有描述的方式:
public ActionResult Index()
{
var docProdivder = Configuration.Services.GetDocumentationProvider();
var desciptions = Configuration.Services.GetApiExplorer().ApiDescriptions;
ViewBag.DocumentationProvider = docProdivder;
return View(desciptions);
}
我在某处读到,如果我这样做:
public void Configuration(IAppBuilder app)
{
// Needs to be first
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
var httpConfig = new HttpConfiguration();
var exploerer = new ApiExplorer(httpConfig);
var descriptions = exploerer.ApiDescriptions;
// Register all areas
AreaRegistration.RegisterAllAreas();
ConfigureOAuthTokenGeneration(app);
ConfigureOAuthTokenConsumption(app);
ConfigureWebApi(httpConfig);
app.UseWebApi(httpConfig);
}
我应该看到描述,但它仍然不起作用。因此,我在其他地方阅读以设置我的项目以输出 xml 描述文件并配置HelpPageConfig文件以使用 documentProvider。我生成了 Xml 描述文件,并且可以验证我的描述是否在其中,这是一个片段:
<member name="T:Melanite.Controllers.CollectionsController">
<summary>
Controller for all collection related functions
</summary>
</member>
<member name="M:Melanite.Controllers.CollectionsController.#ctor">
<summary>
Default constructor
</summary>
</member>
<member name="M:Melanite.Controllers.CollectionsController.Get(System.Int32)">
<summary>
Get all the collections for the given center
</summary>
<param name="centerId">The id of the center that the collection belongs to</param>
<returns>A list of collections</returns>
</member>
<member name="M:Melanite.Controllers.CollectionsController.Get(System.Int32,System.DateTime)">
<summary>
Get all the collections for the given center on a specific date
</summary>
<param name="centerId">The id of the center that the collection belongs to</param>
<param name="date">The planned collection date for the collections</param>
<returns>A list of collections</returns>
</member>
我取消注释了HelpPageConfig中的行,如下所示:
// Uncomment the following to use the documentation from XML documentation file.
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
并确保 XML 文件位于 App_Data 文件夹中。这些名称都是正确的,但是当我运行我的项目时,我仍然没有从 ApiExplorer 中得到任何描述。
所以,正如你所看到的,我已经束手无策了。我希望有人以前遇到过这个问题并且知道如何解决它。如果你这样做,请帮助!