Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle 5.4.0 with ASP.NET MVC WebApi - No documentation is shown inside the swagger webpage

I'm currently trying to use Swashbuckle 5.4.0 inside my ASP.NET MVC Web API application. Whenever I go to the swagger generated page (via rooturl/swagger) the page is correctly loaded, but no methods are shown. Comparing my project with another one which correctly works, I can find any difference in the code. What am I missing?

Here you are the .cs files related to the problem:

SwaggerConfig.cs:

namespace WebApiExperiment
{
    public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration.EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "Swagger Demo Api");
                c.IncludeXmlComments(string.Format(@"{0}\bin\WebApiExperiment.XML",
                                     System.AppDomain.CurrentDomain.BaseDirectory));
            })
                    .EnableSwaggerUi();
        }


    }
}

Startup.cs

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

Global.asax

namespace WebApiExperiment
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

HomeController.cs (the easiest and dumbest controller inside my project)

namespace WebApiExperiment.Controllers
{
    public class HomeController : ApiController
    {


        public IHttpActionResult Index(SendCodeViewModel sendView)
        {
            return Ok();
        }

        public IHttpActionResult About(SendCodeViewModel sendView)
        {

            return Ok("Your application description page.");
        }

        public IHttpActionResult Contact(SendCodeViewModel sendView)
        {


            return Ok("Your contact page.");
        }
    }
}

For any file, just let me know and I'll provide you.

like image 466
Gab Avatar asked Oct 30 '22 18:10

Gab


1 Answers

Short answere: Routing matters. How swagger can generate documentation without knowledge of routing in your API?

Try this:

namespace WebApiExperiment.Controllers
{
    [RoutePrefix("api/routingMatters")]
    public class HomeController : ApiController
    {
        [Route("magic")]
        public IHttpActionResult Index(SendCodeViewModel sendView)
        {
            return Ok();
        }
        [Route("magic2")]
        public IHttpActionResult About(SendCodeViewModel sendView)
        {

            return Ok("Your application description page.");
        }
        [Route("magic3")]
        public IHttpActionResult Contact(SendCodeViewModel sendView)
        {
            return Ok("Your contact page.");
        }
    }
}
like image 145
Sebastian 506563 Avatar answered Nov 15 '22 06:11

Sebastian 506563