5

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.

Gab
  • 87
  • 3
  • 7

1 Answers1

2

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.");
        }
    }
}
Sebastian 506563
  • 6,980
  • 3
  • 31
  • 56
  • Yea it works! Thank you! Which part of the doc have I missed? – Gab Oct 18 '16 at 20:40
  • @Gab hard to say, it is more like your API was not even API. Routing is part of basic WebApi implemenatation. I guess you used Controllers before ApiControllers and this is the reason. So try to read tutorial about WebApi then everything will be clear. Swashbucle gets all public API methods, thats all. – Sebastian 506563 Oct 19 '16 at 06:54