1

I had ASP MVC 5 project, to which I've added Web Api. Now I'm trying to install Swashbuckle (from bootstrap) - but it just shows an empty document without any controllers.

My controller:

 [RoutePrefix("api/v1/Test/Check")]
    public class TestController : ApiController
    {
        // GET: api/Test
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/Test/5
        [HttpGet]
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/Test
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/Test/5
        [HttpPut]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Test/5
        [HttpDelete]
        public void Delete(int id)
        {
        }

        // GET: api/Test
        [Route("GetMetadata")]
        [HttpGet]
        public IEnumerable<string> GetMetadata()
        {
            return new string[] { "value2221", "value2" };
        }
    }

My Web Api Config:

  configuration.MapHttpAttributeRoutes();

  configuration.Routes.MapHttpRoute("Public API V1", "api/v1/{controller}/{id}",
                new { id = RouteParameter.Optional });

My Global.asax.cs:

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

Swashbuckle configuration is default.

The generated swagger document looks like this:

{
  swagger: "2.0",
  info: {
    version: "v1",
    title: "WebServer"
  },
  host: "localhost:55309",
  schemes: [
    "http"
  ],
  paths: { },
  definitions: { }
}

I can access my controller on http://localhost:55309/api/v1/Test

I'm not sure do I need to change anything in the generated SwaggerConfig.cs but looking through it and on swashbuckle docs it looks like it should work without any modifications

Archeg
  • 8,364
  • 7
  • 43
  • 90
  • 1
    you have to put appropriate Attribute on your methods , like HttpPost , HttpGet. try this – osman Rahimi Mar 20 '18 at 15:59
  • @osmanRahimi unfortunately doesn't make any difference. Still doesn't work – Archeg Mar 20 '18 at 16:05
  • @Archeg please show us your attempt at using those attributes. we can't help with what we can't see. –  Mar 20 '18 at 17:21
  • I think you problem is "ASP MVC 5 project" try just with a fresh default WebApi project and add swashbuckle – Helder Sepulveda Mar 20 '18 at 19:10
  • @HelderSepu Could be, but I need ASP MVC 5 + Web Api, so this is a no go. It works if I do it on a separate Web Api only project – Archeg Mar 20 '18 at 19:34
  • @Amy Here you are. But I really doubt it has anything to do with attributes. As I said controller is working, it's only swashbuckle that doesn't see it – Archeg Mar 20 '18 at 19:36
  • Yeah, nothing in your question stands out as the culprit. I notice that your controller isn't called `Test` **`Api`** `Controller`, but that shouldn't be relevant. As far as I'm aware, your question covers all the bases. –  Mar 20 '18 at 19:38
  • The point I was trying to make is that you will need to compare the code (specifically the WebApiConfig.cs that's where all the config and routes are set) from a fresh default WebApi project and yours see what stands out. – Helder Sepulveda Mar 20 '18 at 19:44
  • @Amy Thanks for your support, if you are interested - please see my answer. I'm still not following the root of the problem (your inputs are welcome), but I finally made it work – Archeg Mar 21 '18 at 10:09
  • @HelderSepu Thanks for your support, if you are interested - please see my answer. I'm still not following the root of the problem (your inputs are welcome), but I finally made it work – Archeg Mar 21 '18 at 10:09

2 Answers2

3

Ok, I think I've made it. I had to download swashbuckle sources and debug it, but it was worth it.

Turns out the problem is not swashbuckle itself, but rather ApiExplorer that has .ApiDescriptions empty for some reason.

During my debug I've put these two lines in the end of my Application_Start(), and even though these lines don't do anything, magic happened and it started to work:

var explorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
var descriptions = explorer.ApiDescriptions;

Then I went futher and found this topic: ASP.Net Web API Help Page Area returning empty output (see first answer) I was using Glimpse, although I actually installed it in an attempt to solve the swashbuckle problem! (I thought it might help - it does not, but Glimpse felt a nice tool so I left it there)

As the first answer suggests, I've modified web.config with this:

<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<inspectors>
   <ignoredTypes>
      <add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet"/>
   </ignoredTypes>
</inspectors>
</glimpse>

And removed the two lines from Application_Start() as these are not a fix. It started to work!

To be honest I have no idea what is happening there. I clearly remember it not working before I started to use Glimpse, so installing a glimpse and fixing a problem in it doesn't feel like a proper fix to original problem, but I'm really tired of this issue and am ready to close it down like this.

Hope this helps somebody.

P.S. Just a warning to those who try to debug similar problems. My other mistake was that I didn't close IIS Express between my tries. It actually keeps application running so the configuration is not re-applied even though you start/stop app in VS. If you working with configuration - you need to close ISS Express between your tries.

Archeg
  • 8,364
  • 7
  • 43
  • 90
1

I have faced the same problem in my web api project and I have solved it this way:

1) First of all I have created the following extension method:

 public static class SwaggerExtensions
{
    public static HttpConfiguration EnableSwagger(this HttpConfiguration httpConfiguration)
    {
        httpConfiguration
            .EnableSwagger(c => c.SingleApiVersion("v1", "WebApi"))
            .EnableSwaggerUi();
        return httpConfiguration;
    }
}

2) Then inside Startup class:

 public void Configuration(IAppBuilder app)
    {

        var config = new HttpConfiguration();

        config.EnableSwagger();
        var webApiConfiguration = WebApiConfig.Register(config);

        //here I commented other startup code

    }
Simple Code
  • 2,354
  • 2
  • 27
  • 56