Using ‘UseMvc’ to configure MVC is not supported while using Endpoint Routing.
Issue Description
.NET Core runtime gives below error,
Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing. To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'
Or
System.InvalidOperationException: 'Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false'
Resolution:
The issue I found to be due to updates on the .NET Core framework. The latest .NET Core 3.0 released version requires explicit opt-in for using MVC.
This issue is most visible when one tries to migrate from older .NET Core(2.2 or preview 3.0 version) to .NET Core 3.0
If migrating from 2.2 to 3.0, please use the below code to fix the issue.
services.AddMvc(options => options.EnableEndpointRouting = false);
If using .NET Core 3.0 and above template,
services.AddControllers(options => options.EnableEndpointRouting = false);
ConfigServices method after fix as below,
The issue can be fixed by either of the above approaches.
Why this change?
It’s important to know if the App wants to use IRouter base routing logic or Endpoint based routing logic. If the app requires legacy IRouter support, you need to disable EnableEndpointRouting using the above code.
- IRouter based logic is used in ASP.NET Core 2.1 and earlier version.
- Microsoft however, recommends using Endpoint Routing.
- EmdpointRouting will be set to true if using .NET Core 3.0.
- If using Endpoint routing, it will match HTTP requests to MVC actions.
The above fix also means that you would like to continue to use the previous configuration as is i.e IRouter based routing.
Please note that while enabling for endpoint routing CORS configuration will be different than regular IRouter.
For more details, please refer below few references,
Other references:
That’s all! Happy coding!
Does this help you fix your issue?
Do you have any better solutions or suggestions? Please sound off your comments below.
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
Thanks for the details. My issue resolved.
Thanks. my issue got fixed using above code.
Thanks. This was very useful.
Thanks Morris.
Thanks for writing this up. Much appreciated.
Thank you Damien for your comments !!
Thanks worked for me. I do see many such breaking changes introduced in .NET Core 3.0.
Thanks Jeff for your comments. Agree but I see them more as great improvement..and there are many of such.