I have a web api controller like below.
In swagger output I am having the below image
And when I want to consume it in my another console app, it is showing the below error
Note I have followed the tutorial Customize Swashbuckle-generated API definitions but still getting the same error. Any idea?
I found this while looking for the same question. For me it was a dumb mistake: the controller's methods had the same name. Apparently, Swashbuckle uses the method's name as the Operation Id.
I must need to add the Route Attribute on top of the Action methods. Like below:
So, the API changed into below Swagger UI endpoints
And when I consumed this Swagger in my console app, the below OperationId I got
And I can now call like below (just an example)
Related
I am working on a Web API project using Asp Net Core 2.1.
My existing Web API has the following Get method (and numerous other methods like these):
[HttpGet("GetEmployeByName/{Name}")]
public Employee GetEmployeeByName(string name)
{... some code...}
This responds to the following incoming request:
http://localhost:8080/Employee/GetEmployeeByName/{Name}
Now my requirement is such that some of the APIs could be called by passing an additional id such as:
http://localhost:8080/Employee/{Id}/GetEmployeeByName/{Name}.
I still need to map it to the original method without changing the route.
This can be done using ASP Net Core's feature of URL Rewrite and the URL can be re-written without the id.
However what i want to do is to capture the {Id} as well and save it to Session\ Context.
Does anyone know how to extract the id and rewrite the URL.
Maybe you can find what you need here optional parameters web api, you would need to add that parameter at the end as optional parameters always go at the end, also you could try to use both versions of the end point encapsulate the logic and reuse it on both versions.
Our service uses ASP.NET Core and in the application pipeline, we have several middlewares which are configured in StartUp.cs Configure(IApplicationBuilder app) method.
The middlewares are added by this method:
app.UseMiddleware<Type>();
I would like to validate the HttpContext.Request.Path and make sure it can hit one of the controllers. How can I get the list of available routes (controller path) in the middleware code or is there even a simpler way to see if this certain request path will hit one of the registered controller? We used xxxxcontroller : ControllerBase and a [Route("controller/{version}/{id}] attribute to register the controller.
Thanks a lot.
I suggest you to take a look at Asp.net core identity, If I understood what you`re looking for, you need to use roles to guarantee access to certain routes.
I don't know how to get a list of all routes and check that the path is for a valid route but you can use middleware to check the response status code after MVC runs and if the status code is a 404 then you know it wasn't a valid route and you can handle it accordingly.
The UseStatusCodePagesWithReExecute extension method basically uses this approach to handle not only 404 errors but all error status codes.
I want the best experience for my consumers, and created a swagger endpoint for my function app. I just ran VS2017's add endpoint feature
And got a function call named ApiTokenPolicyPublisherIDSpecificationIDpostAsync ... a concatenation of the URL: /api/TokenPolicy/{PublisherID}/{SpecificationID} method POST + async.
Is there something I can do better with my swagger, or is this how VS just does its code? (if so, that's disappointing)
Here is a link to my swagger
Looks like the VS is converting your operationId in your swagger definition into the method calls
After updating the operation id in your sample swagger
operationId: '/api/TokenPolicy/{PublisherID}/{SpecificationID}/post'
to
operationId: 'CustomEnroll'
This is what i got
Found one example on the whole Internet of the following, and it was on StackOverflow, but there was no follow up whether it had been solved or not. This error happened after I implemented accept header versioning with attribute routing in a WebAPI 2 project.
Message: "An error has occurred." ExceptionMessage: "The given key was
not present in the dictionary." ExceptionType:
"System.Collections.Generic.KeyNotFoundException"
found here on stackoverflow:
How to get controller name when Web API versioning with routing attribues
I am not sure how you implemented this as you have not shared any code, but you can take a look at the following sample demonstrating Web API's attribute routing and versioning through route constraints.
http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/RoutingConstraintsSample/ReadMe.txt
The above sample looks for a custom header called api-version, but you can easily modify this to suit your scenario.
What specifically in WebAPI responds to:
1. http://server/vroot/odata
2. http://server/vroot/odata?$metadata
3. http://server/vroot/odata/Foo
When #3 is requested, I understand that my 'FooController' responds as
configured in my WebApiConfig.cs.
But it is not clear to me how WebAPI responds to #1 or #2. How does it know
what to return? How is that response configured in my code?
UPDATE: Here is a HUGE clue
From http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx
One important thing to realize here is that the controller name, the
action names, and the parameter names all matter. OData controller and
action selection work a little differently than they do in Web API.
Instead of being based on route parameters, OData controller and
action selection is based on the OData meaning of the request URI. So
for example if you made a request for
http://my.server.com/vroot/odata/$metadata, the request would actually get
dispatched to a separate special controller that returns the metadata
document for the OData service. Notice how the controller name also
matches the entity set name we defined previously. I’ll try to go into
more depth about OData routing in a future blog post.
]
returns you the Service Document
returns you the Service Metadata Document
WebAPI knows this because you add a route similar to config.Routes.MapODataRoute("ODataRoute", "odata", model);
Check out this detailed explanation: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/creating-an-odata-endpoint