I try to use Swagger with Microsoft WebAPI 2.
For the moment, I\'ve the following call in a method.
appBuilder
.ConfigureOAuth()
.UseWebApi(con
For ASP.NET Core the following pull request was created: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/486
In the meantime the following workaround can be used:
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;
}
Cheers
Following the example from here:
https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.2&tabs=visual-studio
public class Startup {
public void Configure(IApplicationBuilder app) {
...
app.UseSwaggerUI( c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
app.UseMvc(); // <-- must be after
}
}
I couldn't get it to work until I placed the app.UseMvc() after the call to app.UseSwaggerUI().
If you've come here looking for the asp.net core 2 answer you can acheive the same by setting the RoutePrefix of swagger to the apps root
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My service");
c.RoutePrefix = string.Empty; // Set Swagger UI at apps root
});
How to redirect root to swagger in Asp.Net Core 2.x?
Asp Net Core > 2.2 in a RestFUL API, just set the default url in the Project/Properties/Debug settings
Ok, here is one way of doing it. Add a new MVC controller (Not Web API) e.g HomeController and in the Index action add the following code:
using System.Web.Mvc;
namespace Kids.Math.Api.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return new RedirectResult("~/swagger/ui/index");
}
}
}
Also, make sure your route config has the follow (Note, by default it already does)
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 }
);
}
For Asp.Net core use this:
app.Run(context => {
context.Response.Redirect("swagger/ui");
return Task.CompletedTask;
});