Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have created a WebAPI using .Net 4.5 and want to document this API using Swagger. I have added swagger-ui in my .Net project. Now when i browse to ../swagger-ui/index.html it successfully opens pet store api-docs (json) in swagger UI format.

My question is how can I create such (swagger) json for my WebAPI controllers and models? As I have put in required XML summaries/comments to c# classes and attributes.

I saw that Swagger.Net and Swashbuckle are there doing similar things but I could not really understand how to generate swagger-json file using any of them. There might be very small mistake I am doing but unable to point out.

Please help.

share|improve this question
    
I want to do opposite to this stackoverflow.com/questions/10560857/… –  theGeekster Jan 24 '14 at 15:35
    
Did you find any solution to the question? I'm also interesting in generation of json spec without running a web server. –  Shrike Nov 14 '14 at 12:06
    
No, I couldn't find any solution yet, which supports WebAPI's Attribute Routing. –  theGeekster Nov 20 '14 at 8:21

1 Answer 1

You need to integrate Swagger.NET into your project so that you end up with the following controller:

public class SwaggerController : ApiController { /* snip */ }

and you should also have the following route registered:

context.Routes.MapHttpRoute (
name : "Swagger",
routeTemplate: "api/swagger"
defaults: new
{
  controller = "Swagger",
  action = "Get",
});

assuming that is working you should be able to call /api/swagger and get something like the following:

{
  apiVersion: "4.0.0.0",
  swaggerVersion: "2.0",
  basePath: "http://localhost:5555",
  resourcePath: null,
  apis: [
  {
    path: "/api/docs/Values",
    description: "No Documentation Found.",
    operations: [ ]
  },
  {
    path: "/api/docs/Home",
    description: "No Documentation Found.",
    operations: [ ]
  }
]

}

then in SwaggerUI/index.html you'll want to update the discoveryUrl:

<script type="text/javascript">
    $(function () {
        window.swaggerUi = new SwaggerUi({
            discoveryUrl: "http://localhost:5555/api/swagger",
            apiKey:"",
            dom_id:"swagger-ui-container",
            supportHeaderParams: false,
            supportedSubmitMethods: ['get', 'post', 'put']
        });

        window.swaggerUi.load();
    });
</script>
share|improve this answer
    
That's again runtime version of doc. We have to run a web server to get/show doc. But the question was about how to generate a json spec. I'm also interested in this topic - I need to generate a json Swagger spec file on build. –  Shrike Nov 14 '14 at 12:04
    
Swagger.Net will generate the json spec for you using the ASP.NET ApiExplorer. If you need to save the json spec file for some reason then just call the url and save the results to a file. –  Todd Smith Nov 21 '14 at 19:25
    
Some data will always be available only at runtime, this is why you need to run the service. For instance the routes will be defined by code, so a static analysis would not be able to guess the actual routes without running the service. –  Sébastien Ros - MSFT Jan 22 at 0:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.