aspnet-api-versioning | add service API versioning to ASP.NET Web API | SQL Database library
kandi X-RAY | aspnet-api-versioning Summary
kandi X-RAY | aspnet-api-versioning Summary
Provides a set of libraries which add service API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of aspnet-api-versioning
aspnet-api-versioning Key Features
aspnet-api-versioning Examples and Code Snippets
Community Discussions
Trending Discussions on aspnet-api-versioning
QUESTION
I'm using ASP.NET Core with the ASP.NET API Versioning service.
When I register the service, I use this format:
...ANSWER
Answered 2021-Oct-21 at 13:53This can be retrieved from the container like so:
QUESTION
I have a simple ASP.NET 5 Web API with a controller for uploading/downloading files, like:
...ANSWER
Answered 2021-Jul-15 at 20:30When using overload CreatedAtAction(string, object)
, param object
is not route parameters as you assume, but body content. Hence, code is looking for a route to GET api/contents which doesn't exists.
You'll need to use overload CreatedAtAction(string, object, object)
where param #2 contains route params, and param #3 contains body content. So in your case, return CreatedAtAction(nameof(DownloadContent), new { id = id }, null);
will work.
I was able to reproduce your issue with this controller and Postman:
QUESTION
I'm looking into adding versioning to an existing API we have. We are embedding the version in the URL.
The requirement for the versioning is that it should be easy to add a new version, but not everything changes and we don't want to go through all controllers and add new version attributes when we get a new version. Is there any way to tell Microsoft.AspNetCore.Mvc.Versioning that a particular method should be available regardless of versions?
I have tried decorating my method with both with the version:apiVersion and with just a basic route.
...ANSWER
Answered 2021-Jun-29 at 17:40Setting AssumeDefaultVersionWhenUnspecified = true
is a highly abused feature. This is only meant to facilitate backward compatibility. Once you opt into API Versioning, all API controllers have some implicit or explicit API version. Assuming a version provides a mechanism to handle the "original", unnamed version that would otherwise break existing clients that don't know to include an API version in requests.
"Is there any way to tell Microsoft.AspNetCore.Mvc.Versioning that a particular method should be available regardless of versions?"
Yes, but I'm not entirely sure that is what you're looking for. An API can be version-neutral, which means it will accept any and all API versions, including none at all. This is useful for certain types of APIs such as a /ping
health check or DELETE
, which typically do not change over time. Such an API can be decorated with [ApiVersionNeutral]
. This can be for all APIs on a controller or a specific action. Note that once you choose this path, "there can be only one."
There is not a complete picture of your setup. It appears that you are adding API versioning to an existing set of APIs. One of the primary purposes of DefaultApiVersion
is to set what the default API version should be when no other information is available. If you applied no API versions whatsoever - for example, using attributes, then this would be the implicit API version of all APIs. This prevents you from having to retrofit all of your existing controllers and code. What's less obvious is that once you apply any explicit API version, then the implicit rule is ignored. This ensures that implicit versions can be sunset. The default version often does not matter. It's simply a way for you to indicate what the initial API version originally was. It's most relevant when grandfathering in existing APIs.
Given your configuration:
QUESTION
I have an ASP.NET Core 3.1 API and I am introducing a new version for one of my controllers. I am using the Microsoft.AspNetCore.Mvc.Versioning NuGet package and I have set the new version to be the default version. All my other controllers should work with both the old version (1.0) and the new version (1.1).
For example:
...ANSWER
Answered 2021-Mar-23 at 19:08Q: Do I really have to add all versions to all controllers?
A: Yes. API versions are discrete. A client should get exactly what they ask for and an appropriate error response if the server cannot satisfy the request.
Q: Is there a better/correct way of handling this?
A: There are several possible options. A better or more correct way of handling things is highly subjective. There are numerous factors and the final decision may simply come down to preference.
A common misconception is that API Versioning uses attributes. It really doesn't care about attributes, it's just one possibility and one that tends to resonate with developers as metadata. You have the choice to use out-of-the-box attributes, custom attributes, out-of-the-box conventions, or your own custom conventions.
The choice of how to apply the version metadata is typically of preference and practical management. A common scenario is to organize controllers in folders by API version. In several .NET languages, the folder name translates to part or all of the corresponding namespace. This arrangement is common enough that there is an out-of-the-box VersionByNamespaceConvention for it. For details, see the documentation. The By Namespace Sample also demonstrates how to up such a project end-to-end.
API version-neutral means that an API takes any and all versions, including none at all. It can mean you don't care about the API version or that you accept a whole range that you'll deal with yourself. It is really only meant to be used with APIs that never change over time. For example, a HTTP DELETE
operation typically does not change over time or across versions.
It's not 100% clear what you mean by:
"If I do not add the [ApiVersion] attribute, it defaults to the new 1.1 version and 1.0 no longer works."
There are no defaults per se. This statement seems to imply that you have set the AssumeDefaultVersionWhenUnspecified
option to true
. You should not do that unless you have a very good reason to do so. That is probably one of the most abused features of API Versioning. A client must know the version it is asking for. Allowing a client to not specify a version and having things transition from 1.0
to 1.1
can break the client. The server can make no assumption that it won't. This feature was meant to grandfather in existing services that didn't previously have an explicit version defined. That scenario only exists when API Versioning is first enabled. As stated above, all controllers must have one or more discrete API versions, but the original set of APIs didn't have it explicitly defined. If this feature didn't exist, then the baseline set of clients that didn't know about an API version would break.
QUESTION
EDIT: here's a repo which simulates the error.
I'm building an API using ASP.NET Core together with ApiVersioning. My controllers are annotated with [Route("api/v{version:apiVersion/[controller]
and on my POST actions I'm returning the location of the created resource:
ANSWER
Answered 2020-Aug-12 at 20:02It's solved, I'm leaving it here for future generations.
When calling CreatedAtAction
, you must inform the version
of the action you want to call. While this may be obvious, there's an exact way to do so:
Either capture the current version and pass it back:
QUESTION
I am introducing API versioning to an existing API. The existing JSON uses Pascal casing for its property names e.g. "FooBar": "foo". For v2 of the API, I would like to use the common camel casing, "fooBar": "foo". I need to keep v1 Pascal casing so that it does not impact any client that is already pulling that version of the API.
My project is
- ASP.NET MVC 5.2.7
- ASP.NET WEB API 5.2.7
- ASP.NET ODATA 7.4.0
- ASP.NET WEB API Versioning 4.0.0
My configuration is as follows
...ANSWER
Answered 2020-Jun-01 at 13:50Using the OData Model Configurations approach described here
first add a class that derives from IModelConfiguration
to your project.
Something like this:
QUESTION
I would like to add a custom header to my service's response if a deprecated version is requested.
I already have URL-based versioning set up using Microsoft.AspNetCore.Mvc.Versioning
, and I additionally have an existing custom ActionFilter
class that can write custom headers into the response. I can also get the version requested by the client using context.HttpContext.GetRequestedApiVersion()
inside my ActionFilter
's definition for OnActionExecuted(ActionExecutedContext context)
.
However, I'm not sure how I can check whether the requested version is a deprecated version or not from my custom ActionFilter. The documentation on deprecating a service version does not answer this question and I can't find the answer among any of the rest of the documentation on github.
My controller class is annotated as follows:
...ANSWER
Answered 2020-Feb-26 at 16:40From the incoming request you will see what the URL is, and therefore you can determine which controller it will be mapped to. Controllers are classes, and the deprecated ones are marked with the [ApiVersion(Deprecated = true)]
attribute. So you can grab the controller, and with reflection you can check its attributes and see if it's deprecated. Documentation here
QUESTION
I have GetAllStudents()
in 2 versions (GetAllStudents()
and GetAllStudentsV2()
).
How to manage versioning in repositories via EntityFramework Core? just like what we have as ASP.NET API Versioning
Can we have versioning at the repository level? (EF/Dapper/...)
...ANSWER
Answered 2020-Feb-21 at 17:37You generally do not version EF because - you still have only one database anyway.
You version the API because it is a "public interface", not an implementation detail. But the db is internal and IS an implementation detail.
Which is why most people will not expose EF objects via an API - they will project them to API objects (which is a way you can handle changes in the db when versioning).
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install aspnet-api-versioning
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page