2015-05-04 27 views
15

使用揚鞭作爲IAppBuilder的歡迎頁面我嘗試使用Swagger與微軟的WebAPI 2.如何的WebAPI

目前,我已經在方法下面的調用。

appBuilder 
    .ConfigureOAuth() 
    .UseWebApi(configuration) 
    .UseWelcomePage(); 

如果我想用揚鞭,我必須使用此網址「https://localhost:44300/swagger」其中一個工作得非常好。

我想我的主頁重定向到我的招搖吧的URL,也許如下,但這個示例不起作用。

appBuilder 
     ... 
     .UseWelcomePage("/swagger"); 

任何想法?

+0

我想類似的事情,你得到這個最後的工作,我嚴格使用它的方式,但不起作用... – user1829319

回答

32

我得到這個工作,我如何想通過增加像這樣在RouteConfig.cs路線:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapHttpRoute(
      name: "swagger_root", 
      routeTemplate: "", 
      defaults: null, 
      constraints: null, 
      handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger")); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

從swashbuckle看到這個代碼,看看發生了什麼事情:https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/Application/RedirectHandler.cs

+0

使用ASP.Net WebRole時非常適合我。 – HaveSpacesuit

+0

這隻適用於默認設置。如果您想要自定義您的路線以使用swagger UI,請勿複製/粘貼。 –

1

您可以在您的配置對象中設置一些路由。由於您的代碼片段有限,因此難以完整說明詳細信息。 希望這點能讓你朝正確的方向發展。

4

好的,這是一種方法。添加一個新的MVC控制器(而不是web API)如HomeController中,並在索引操作添加以下代碼:

using System.Web.Mvc; 

namespace Kids.Math.Api.Controllers 
{ 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return new RedirectResult("~/swagger/ui/index"); 
    } 


} 

}

此外,請確保您的路由配置有如下(請注意,由默認情況下它已經這樣做)

 public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
8

在配置的Startup.cs文件(IAppBuilder APP)方法我用這行代碼,以使其負載重定向到招搖歡迎頁面。

app.Run(async context => { 
    context.Response.Redirect("swagger/ui/index"); 
}); 

所以我使用完整的方法如下

[assembly: OwinStartup(typeof(AtlasAuthorizationServer.Startup))] 
namespace AtlasAuthorizationServer 
{ 
    public partial class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      ConfigureAuth(app); 

      HttpConfiguration config = new HttpConfiguration(); 
      WebApiConfig.Register(config); 
      app.UseWebApi(config); 

      app.Run(async context => { 
       context.Response.Redirect("swagger/ui/index"); 
      }); 
     } 
    } 
} 

請注意,這會導致在Visual Studio中的綠色警報。我確信有一些方法可以將此模擬爲在函數中等待調用的異步方式。

+1

這是最瘦的選擇。它不需要你添加'System.Web.Mvc'不像其他的答案。 –

0

我有類似的問題,我通過自定義SwaggerUI url解決了它。 這是我的配置方法:

public void Configuration(IAppBuilder app) 
{ 
    var thisAssembly = typeof (Startup).Assembly; 

    HttpConfiguration httpConfig = new HttpConfiguration(); 

    app.MapHttpAttributeRoutes(); 
    app.UseCors(CorsOptions.AllowAll); 
    app.UseWebApi(httpConfig); 

    httpConfig 
     .EnableSwagger("api/{apiVersion}",c => 
     { 
      c.IncludeXmlComments(string.Format(@"{0}\bin\Docs.xml", AppDomain.CurrentDomain.BaseDirectory)); 
      c.SingleApiVersion("v1", "My API"); 
     }) 
     .EnableSwaggerUi("{*assetPath}",c => 
     { 
      c.CustomAsset("index", thisAssembly, "AspNetIdentity.WebApi.DocsAssets.index.html"); 
     }); 

    httpConfig.Routes.First(x => x.RouteTemplate == "{*assetPath}").Defaults["assetPath"] = "index"; 
} 

這樣,當你去localhost:44300你會得到揚鞭UI作爲啓動頁。

2

對於Asp.Net核心使用這個:

app.Run(context => { 
      context.Response.Redirect("swagger/ui"); 
      return Task.CompletedTask; 
     }); 
+0

與最新版本,我只是重定向到「招搖」而不是「招搖/用戶界面」 - 儘管工作。 –

0

對於ASP。NET核心下列拉請求創建: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/486

與此同時以下解決方案可用於:

public static IApplicationBuilder UseSwaggerUI(
     this IApplicationBuilder app, 
     Action<SwaggerUIOptions> setupAction) 
    { 
     var options = new SwaggerUIOptions(); 
     setupAction?.Invoke(options); 

     // This method reads an internal property value 
     // http://dotnetfollower.com/wordpress/2012/12/c-how-to-set-or-get-value-of-a-private-or-internal-property-through-the-reflection/ 
     var indexSettings = options.GetPropertyValue<IndexSettings>("IndexSettings"); 
     // Serve swagger-ui assets with the FileServer middleware, using a custom FileProvider 
     // to inject parameters into "index.html" 
     var fileServerOptions = new FileServerOptions 
     { 
      RequestPath = string.IsNullOrWhiteSpace(options.RoutePrefix) ? string.Empty : $"/{options.RoutePrefix}", 
      FileProvider = new SwaggerUIFileProvider(indexSettings.ToTemplateParameters()), 
      EnableDefaultFiles = true, 
      StaticFileOptions = 
      { 
       ContentTypeProvider = new FileExtensionContentTypeProvider() 
      } 
     }; 
     app.UseFileServer(fileServerOptions); 

     return app; 
    } 

乾杯