C# 使Web API 2自宿主支持自动生成“API说明”页面

C# 使Web API 2自宿主支持自动生成“API说明”页面,c#,asp.net,asp.net-mvc,asp.net-web-api,C#,Asp.net,Asp.net Mvc,Asp.net Web Api,我最初使用VS2013向导创建了ASP.NET Web API 2,该向导是IIS express托管时的默认向导,它有一个名为HomeController的默认控制器: public class HomeController : Controller { public ActionResult Index() { ViewBag.Title = "Home Page"; return View(); } } 它通过以下url和UI进行

我最初使用VS2013向导创建了ASP.NET Web API 2,该向导是IIS express托管时的默认向导,它有一个名为
HomeController
的默认控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page";

        return View();
    }
}
它通过以下url和UI进行操作:

单击API链接,该链接提供描述所有API控制器接口的摘要报告,然后用户开发人员知道如何调用这些API:

后来,我试图将此项目切换到self-host,我基本上什么也没做,只是将项目类型更改为Console,并添加
程序.cs
,最后添加了一个新的
SelfHostStartup.cs

public class SelfHostStartup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        var config = new HttpConfiguration();
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        ConfigureAuth(appBuilder);

        // Web API routes
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        // !possible! the HOME page route settings.
        RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        RouteTable.Routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );
        appBuilder.UseWebApi(config);
    }
运行控制台程序后,通过在浏览器中进行测试,所有API控制器都工作正常,但使用这些URL再次进行测试


  • http://localhost:9000/home/index

    显示空白页
  • http://localhost:9000/api/home/index
未找到与请求URI“”匹配的HTTP资源。 找不到与名为“home”的控制器匹配的类型。


  • http://localhost:9000/home/

    显示空白页
我猜这与html和js文件加载有关,似乎自宿主模式没有加载这些文件


有人可以帮忙吗?

我做了一些工作,通过添加另一个控制器来实现类似的功能,并向其中添加一个函数来列出所有现有的API:

public class CloudApiParameter
{
    public string ParameterName { get; set; }
    public string ParameterType { get; set; }
}

public class CloudApiDescription
{
    public string Id { get; set; }
    public string RelativePath { get; set; }
    public string Document { get; set; }
    public IEnumerable<CloudApiParameter> Parameters { get; set; }

}

public IEnumerable<CloudApiDescription> Get()
{
    var apiDescs = Configuration.Services.GetApiExplorer().ApiDescriptions;
    return apiDescs.Select(r => new CloudApiDescription()
    {
        Id = r.GetFriendlyId(),
        RelativePath = r.RelativePath,
        Document = r.Documentation,
        Parameters = (r.ParameterDescriptions.Any() ? r.ParameterDescriptions.Select(p => new CloudApiParameter()
        {
            ParameterName = p.ParameterDescriptor.ParameterName,
            ParameterType = p.ParameterDescriptor.ParameterType.ToString()
        }): null)
    });
}
公共类参数
{
公共字符串参数名称{get;set;}
公共字符串参数类型{get;set;}
}
公共类描述
{
公共字符串Id{get;set;}
公共字符串相对路径{get;set;}
公共字符串文档{get;set;}
公共IEnumerable参数{get;set;}
}
公共IEnumerable Get()
{
var apiDescs=Configuration.Services.GetApiExplorer().apisdescriptions;
返回apiDescs.Select(r=>newcloudapiscription()
{
Id=r.GetFriendlyId(),
相对路径=相对路径,
文件=r.文件,
Parameters=(r.ParameterDescriptions.Any()?r.ParameterDescriptions.Select(p=>newcloudapipParameter())
{
ParameterName=p.ParameterDescriptor.ParameterName,
ParameterType=p.ParameterDescriptor.ParameterType.ToString()
}):null)
});
}

http://localhost:9000/home/index
在您的设置中不存在。您应该针对
http://localhost:9000/api/home/index
屏幕截图中显示的页面是
http://localhost:9000/help/index
not
home/index
…最后,有一个链接可以实现这一点: