Cannot See ODataController Derived Action URLs in Swagger UI - c#

I am currently trying to use Swagger UI to interact with my OData enabled Web API.
Versions for the bits and bobs are as follows:
.Net 4.5.2,
Web API 2.2,
NuGet package Microsoft.AspNet.OData 6.0.0 for OData 4.0 endpoints,
Swashbuckle 5.3.2,
Swashbuckle.Core 5.3.2,
Swashbuckle.OData 3.0.0.
My application has a SwaggerConfig.cs and a WebApiConfig.cs as expected. And I have followed many of the instructions in this article, this article and a few others on the web.
When I launch my API through Visual Studio, I initially get a new browser tab with the local host URL. If I append the word "/swagger" (no quotes) at the end of that URL, I get a nice Swagger UI in the same tab. This tab shows the name of of my API but not much else. I don't see any of the individual URL interfaces for the Actions exposed by my ODataController derived Controller. Moreover, underneath the displayed name of the URL, all I have is [ base url: , api version: v1 ] where "v1" is coming in from SwaggerConfig.cs (same with the API name).
I know I can talk to the API from Swagger UI because I can arrive at the "Get" action of the Controller by modifying the displayed URL appropriately. The actions created were the standard Get, Put, Post, Patch, Delete ones. But I cannot see any of these in the Swagger UI.
This is because I am new to this and obviously have missed out or miscoded stuff to link everything correctly. I have made very little change to SwaggerConfig.cs and left most of the things as they were when it was created. The significant change I have made was adding this line:
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c, GlobalConfiguration.Configuration));
Based on this info, if anyone can guide me in the direction of what pieces could be missing or developed incorrectly, I would greatly appreciate it. If you had any further questions for me, I would be happy to respond in the comments.

It turns out that because I had been recreating the application, Entity Framework was no longer creating the database for me since it was trying to use a name it had already recently used (a known EF bug). So I refractored the name of the database to something else. After this, things were plain sailing.
This negates my comment: "I know I can talk to the API from Swagger UI because I can arrive at the "Get" action of the Controller by modifying the displayed URL appropriately. The actions created were the standard Get, Put, Post, Patch, Delete ones. But I cannot see any of these in the Swagger UI."
Clearly this was me not understanding the issue clearly. But by going through the steps again one by one, I was able to find the problem and come to the desired place :)

Related

How to find the controller/action which maps to an attribute route?

Given a URL, and I want to know which action in which controller is responsible for handling that URL. What I do right now is that I search for the route name in the project from visual studio. But I think there might be a package or tool that lists the routes and their corresponding Controller actions.
Is there a more neat way to find that:
url/examples/1
is handled by:
[HttpGet]
[Route("examples/{id}")]
public Task viewExmaple()
Try Swagger, as I think this will be as close as you can get in terms of "lists the routes and their corresponding Controller actions". By default, swagger acts a GUI (web page) that displays every controller with corresponding controller methods nested beneath(accordion style).
Swagger is also a great tool for debugging and testing. It displays details like which HTTP verb the methods use, which query string params (or JSON payload in body) the method accepts. Great for documentation as well.
Behind the scenes, swagger builds one giant JSON payload that nests all of your controller / controller actions so you may also be able to view it like that.
You would access it by hitting http(s)://your.app.path/swagger
Setting up in a .NET Standard (Non-Core) app

How to customize swagger requests in ASP.NET Core?

I have Swagger setup for an ASP.NET Core 2 MVC API application. I'm using OpenIddict for OAuth but want to customize how the request and responses appear on the documentation.
Since the method in the controller takes an OpenIdConnectRequest, the generated default output looks like so:
... and it just goes on and on.
This is a far cry from the neat JSON required for a client to consume since the middleware does a bunch of work inbetween the client and the controller method.
How do I change how Swagger represents these? I am already using a hack to massage the responses via a custom, private type for token responses, so any help on how to use that would also be appreciated. I have tried to use the SwaggerGenOptions.MapType<> function as the documentation claims that tells Swagger how to map a type to a custom output. Unfortunately, I've not gotten the Swagger output to reflect anything I've done with .MapType<>.
To be clear, these aren't models I control so I can't decorate the members myself.
Note that this is different from How to show WebApi OAuth token endpoint in Swagger. My controller action is discovered fine. Unfortunately, I'm thinking it may be easier to filter it out and use that as another work around to define it manually but I'd rather not if possible.

How can view the swagger ui for Azure API app

I am creating an Azure API app on Visual Studio 2015. when i hit browse and redirected to http://localhost:3012/ if i add swagger to the url nothing happens : http://localhost:3012/swagger
it seems I need to add the /docs/v1 for a full address : http://localhost:3012/swagger/docs/v1 . Shouldn't there be like an automatic URL routing when i add /swagger to load swagger page.
Also, i am only able to view the json schema, if i browser to the UI http://localhost:3012/swagger/ui the page doesn't load.
The API app builds successfully. is there anything missing?
In the configuration of your WebAPI project (SwaggerConfig.cs in the folder App_Start), you have to enable the Swagger UI. It is commented out as default behavior.
Just open the configuration, search for:
/*
})
.EnableSwaggerUi(c =>
{
*/
and disable the comments in the lines above and under it
Pedro, above, has provided a perfect answer for enabling SwaggerUi.
As for your question regarding the URL "swagger/docs/v1"
This is the default URL used by Swashbuckle to return Swagger 2.0 JSON metadata for the API
The SwaggerConfig.cs file is created when you install the Swashbuckle package in a project. You can find it in the folder "App_Start" . It provides a number of ways to configure Swashbuckle. I haven't checked if you can change that default URL or do URL rerouting for it.
Edited:
The default route templates for the Swagger docs and swagger-ui are "swagger/docs/{apiVersion}" and "swagger/ui/{*assetPath}" respectively. You can change these so long as the provided templates include the relevant route parameters - {apiVersion} and {*assetPath}.
For example: the URL to swagger-ui will be myswag/index.
httpConfiguration
.EnableSwagger("docs/{apiVersion}/swagger", c => c.SingleApiVersion("v1", "A title for your API"))
.EnableSwaggerUi("myswag/{*assetPath}");
You can read more about it here in the GitHub repo: https://github.com/domaindrivendev/Swashbuckle
If you are creating API using Template Azure API App in visual studio, you just need to un-comment few lines from SwaggerConfig.cs file to activate Swagger for your API application.
In your solution look for the file 'SwaggerConfig.cs'
'VSSolution' > App_Start > SwaggerConfig.cs
Look for EnableSwaggerUi and remove comments for below line of code
/*
})
.EnableSwaggerUi(c =>
{
*/
That's it, No Other Configuration Required.
Just run the application :
http://localhost:57452/swagger
I had this same problem and was misled a bit by these answers although I saw that they were admittedly entirely correct in the end.
Note the following:
Don't mistake the .EnableSwagger setting for the EnableSwaggerUi
setting in SwaggerConfig.cs.
The former is on by default and comes at the beginning of the
SwaggerConfig.cs file
the latter is commented out by default and buried in a lot of other comments. It cost me quite some time before I noticed that there were actually two settings.
The .EnableSwagger setting causes raw JSON to be emitted on your application url https://yourapp.azurewebsites.net/swagger/docs/v1 - which is not a great help.
While the .EnableSwaggerUi setting is what causes the Swagger UI to appear on your url https://yourapp.azurewebsites.net/swagger

ASP.NET Web API Help Generation Stopped - How to restart Auto Generation?

I have added ASP.NET WEB API Help Documentation to an existing Project.
Its working to an extent, as I can browse to localhost/help and I can see the Account Controller API that comes with ASP.NET MVC.
I can also see one other API controller that I have built, and its Methods.
However I have since added another 5 API controllers, and WEB API Help is failing to generate the documentation for these.
I have enabled these settings in APP_Start/HelpPageConfig of API help:
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
config.SetActualRequestType(typeof(string), "Values", "Get");
config.SetActualResponseType(typeof(string), "Values", "Post");
And I also have enabled XML Documentation Output in the Project Properties/Build Section as mentioned in this post:
Creating Web API Help Pages
So how can I get the documentation to Automatically generate for the other 5 controllers?
Any ideas are vary welcome, and thank you in advance.
Just figured it out, so ill post an answer - hopefully it helps someone else.
I simply needed to register the Controller routes specifically in the App_Start\WebAPIConfig.cs file, and the Help Generation has resumed generating.

Google-drive example: oauth2callback not found

I need a little help with the C# example program of Google-Drive...
I used this so-called "tutorial"/"example":
https://developers.google.com/drive/examples/dotnet
And the code from here:
https://code.google.com/p/google-drive-sdk-samples/source/checkout
I uploaded my (only slightly modified) sourcecode here in case anybody doesn't have Mercurial (I didn't have Mercurial and no admin rights to install it either, and Mercurial is the only way to get the sourcecode...):
http://verzend.be/elt0k13enraw/DrEdit.rar.html
I always get
"Ressource cannot be found"
Requested URL: /oauth2callback
I don't find this astonishing, as no oauth2callback controller or handler is implemented...
I tried adding a Controller called oauth2callbackController and redirecting to another action in oauth2callbackController.Index, doing
return new RedirectResult("/about/about");
But that only creates a NULL-reference exception.
So i figured, maybe the wrong controller and redirected to
return new RedirectResult("/drive/Index");
But that only creates an infinite loop of redirect -> allow -> redirect - allow -> etc.
BTW, the config to change the API key + REDIRECT_URI is in
Models\ClientCredentials.cs
Note:
The problem aren't my modifications.
The sample also didn't work unmodified, with the exact same error.
All I did was removing EntityFramwork references, and throwing "Not implemented exception" when a method using entity was called.
Edit:
Additional information:
What I really wanted to do in the first place is to write a console service that exports my database, LZMA-compresses the exported content, encrypts that with OpenPGP, and uploads the database of my server to Google drive every day at 24:00 o'clock, without any user input.
I got export working without a problem, i got the LZMA compression working without a problem, I got the encryption with PGP working without a problem.
After the end of the working day (grrrr), when I was at home, I was even able to download the example-code with the mercurial installed on my Linux-machine at home, and bring it on the windows machine using SMB...
But now I can't get the sample for the Google-drive SDK working...
And moreover, what I really need is an example for a console service/daemon, not a web-application.
When I created the API key, I saw one could create a key for a service, but there is no example on how to write a Google-Drive service (console application), and no useful documentation as well (yea there is a reference, but it's only a reference, IntelliSense provides about the same)...
When configuring your app in the API Access tab of the APIs Console, you had to set the root (/) of your web server as the redirect URI and not /oauth2callback.
Assuming that your app is published at www.example.com, just go back to the APIs Console and set it to www.example.com instead of www.example.com/oauth2callback

Categories

Resources