我有这个项目只是一个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的描述。
所以,你可以看到我在我的智慧结束。我希望有人之前遇到过这个并且知道如何修复它。如果你这样做,请帮忙!
答案 0 :(得分:1)
我有同样的问题。如果我添加了
GlobalConfiguration.Configure(WebApiConfig.Register)
在Startup类中(我不使用global.asax)一切正常。我希望这对你也有帮助。
答案 1 :(得分:0)
如果您无法访问WebApiConfig.Register,我没有使用我的Owin WebApi项目,以下代码似乎对我有用。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.core</groupId>
<artifactId>tester</artifactId>
<version>${project.version}</version>
<packaging>war</packaging>
<name>tester</name>
<description>Testing Spring Boot on EB</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.9.BUILD-SNAPSHOT</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<apache-commons.version>3.0</apache-commons.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache-commons.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<!-- tag::wsdl[] -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>integration.wsdl</generatePackage>
<schemas>
<schema>
<url>https://www.soaplayeroverhere.com?wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
<!-- end::wsdl[] -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>