Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger w/ ASP.NET v5 Azure Api App

I'm attempting to set up a Api App (Azure) with Swagger + Swashbuckle as demonstrated by Scott Hanselman at the //Build conference here: http://channel9.msdn.com/Events/Build/2015/2-628.

I have installed (using NuGet) the packages Swagger.Api and Swashbuckle.Core. It hasn't added any controller or settings that I would expect in order to have a swagger page. When I navigate to {baseUrl}\swagger, I get a 404 error.

I would think that since it has a UI it would require a Web App in addition to the Api App, but I've rewatched the demo and Scott clearly says you can add Swagger + Swashbuckle to any Api App. In a 2nd app though I'd think there may be issues with Api discovery. Has anyone set this up yet successfully?

Project with swagger

like image 392
Ryan Langton Avatar asked May 05 '15 14:05

Ryan Langton


2 Answers

Time rolls on and now Swashbuckle works for vNext (beta8 for me, probably other versions too) - thank you to the team and contributors!

In project.json add the package:

    "Swashbuckle": "6.0.0-*",

In startup.cs in ConfigureServices():

        services.AddSwaggerGen();
        services.ConfigureSwaggerDocument(options =>
        {
            options.SingleApiVersion(new Info
            {
                Version = "v1",
                Title = "My Super API",
                Description = "A Super API using Swagger and Swashbuckle",
                TermsOfService = ""
            });

        });
        services.ConfigureSwaggerSchema(options =>{
            options.DescribeAllEnumsAsStrings = true;
        });

In startup.cs in Configure():

        app.UseSwaggerGen();
        app.UseSwaggerUi();

Now you can access your API doco - http://domain:port/swagger/ui/index.html

Access your Swagger definition - http://domain:port/swagger/v1/swagger.json

Then assuming you have at least one internet facing dev/uat/staging/prod environment, grab the definition URL then do File-> Import URI at http://editor.swagger.io/ - now you have code-gen for about 20 clients too :)

You can also setup your own code-gen server if you are so inclined too (https://github.com/swagger-api/swagger-codegen), however I leveraged the existing online generator. The online editor also has full model and relationship definitions too at least in my case where I fully defined my model using EF7 (I know, ick... but it's much better than <= EF6 and ServiceStack doesn't support CoreCLR, yet). Depending on the size of your project this could save you a few hours to a few weeks of work documenting, plus it is dynamically updating itself as you code more. And you can use it to test your endpoints too, but I still prefer PostMan.

Full sample project at https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample/tree/beta8

Big ups to MrSheepUK

HTH

like image 72
Ben E G Avatar answered Sep 23 '22 05:09

Ben E G


This answer is now outdated. See the other answer.

There is a nice blogpost describing the problem you have: https://alexanderzeitler.com/articles/Deploying-a-ASP-NET-MVC-6-API-as-Azure-API-App-in-Azure-App-Services/

This describes how you can add the Ahoy! package to an ASP.NET v6 Web Api project and adding this as an API app to Azure.

Here is another source: http://devmeetsbi.ghost.io/help-and-test-page-for-asp-net-web-api-asp-net-5-and-mvc-6/

like image 28
Wessel Kranenborg Avatar answered Sep 20 '22 05:09

Wessel Kranenborg