Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is SetCompatibilityVersion inside of the startup class of asp.net Web API core project

Using Visual Studio 2017, I just created a simple API project as shown below. And in the Startup.cs file I have this code.

public void ConfigureServices(IServiceCollection services) {      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);  } 

Can someone please throw some light as to what is means? Do we need to keep this code?

I think MS should put some comments to indicate what such code does.

SetCompatibilityVersion

like image 826
VivekDev Avatar asked Jan 15 '19 06:01

VivekDev


People also ask

What is the startup class in ASP.NET Core?

ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class: Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality.

What is Startup class in Web API?

The Startup class is the entry point to the application, setting up configuration and wiring up services the application will use. Developers configure a request pipeline in the Startup class that is used to handle all requests made to the application. Sections: The Startup class.

What is the role of startup class in .NET Core?

The Startup class in .NET and .NET Core The Startup class contains the ConfigureServices and Configure methods. While the former is used to configure the required services, the latter is used to configure the request processing pipeline. The Configure method is executed immediately after the ConfigureServices method.

What is Startup CS in Web API?

Startup. cs file contains Startup class which triggers at first when application launches and even in each HTTP request/response. Actually, the inception of Startup class is in OWIN application which is a specification to reduce dependency of application on server.


1 Answers

When you call the AddMvc method, several components are registered with certain options. You call one method and the whole mvc framework is wired up.

However, if the mvc team in the future decides to change a default value, or decides that a component is no longer to be registered by default, or changes an expected side effect of this method, the user code relying on that would break. To avoid such breakage, you can call the set compatibility method which the mvc team will use to preserve the behavior provided to you.

Suppose they introduce a new feature, which exists only when you are targeting the 2.3 platform: if your code declares that it targets the 2.2 api, the mvc team will know that you are not using that feature because it was not existing at that time. This way the can make safe assumptions about what should be provided and how.

For further details, please look at MSDN.

like image 131
Yennefer Avatar answered Sep 22 '22 14:09

Yennefer