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

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.


Yennefer's answer is great, i'll just add some things.

  • For ASP.NET Core 2.X it might very well be needed to use SetCompatibilityVersion, for the reasons Yennefer described.

  • For ASP.NET Core 3.0 it's a no-op, meaning it does absolutely nothing. Read more here.

  • For ASP.NET Core 3.X (but not 3.0) and forward, it might have a value. Read more here