18

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.

4
  • I want to do opposite to this stackoverflow.com/questions/10560857/… Commented Jan 24, 2014 at 15:35
  • 1
    Did you find any solution to the question? I'm also interesting in generation of json spec without running a web server. Commented Nov 14, 2014 at 12:06
  • No, I couldn't find any solution yet, which supports WebAPI's Attribute Routing. Commented Nov 20, 2014 at 8:21
  • For a .NET 5++ project using Swashbuckle.AspNetCore, you can find the OpenAPI .json file in <my project>\obj\Debug\net8.0\EndpointInfo Commented Nov 20 at 22:05

3 Answers 3

27

As stated, /swagger takes you to the swagger UI.

If you're using Swashbuckle, then /swagger/docs/v1 should take you to the swagger.json file - I found this using Chrome Dev tools.

Edit: if you're using Swashbuckle.AspNetCore, then the url is slightly different - /swagger/v1/swagger.json

Sign up to request clarification or add additional context in comments.

1 Comment

Thats if you are using Swashbuckle.
4

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>

3 Comments

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.
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.
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.
1

You can use "NSwagStudio" desktop application to load the json document without running the api project. By providing the api assembly.

https://github.com/RSuter/NSwag/wiki/NSwagStudio

Download the (NSwagStudio) windows desktop application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.