I was upgrading the project framework from ASP.NET Core 2.1 to ASP.NET Core 3.1 and I got the below exception error in my Startup.cs class.
Error Details
“System.InvalidOperationException HResult=0x80131509 Message=Endpoint Routing does not support ‘IApplicationBuilder.UseMvc(…)’.
To use ‘IApplicationBuilder.UseMvc’ set ‘MvcOptions.EnableEndpointRouting = false’ inside ‘ConfigureServices(…). Source=Microsoft.AspNetCore.Mvc.Core StackTrace: at
Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app,
Action`1 configureRoutes) at ProjectName.Startup.Configure(IApplicationBuilder app,
IHostingEnvironment env) in ProjectDirectoryPath\Startup.cs:line 89 at
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean
constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at
Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.
<Build>b__0(IApplicationBuilder builder) at
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)”
When I continued to run the project, the page loaded with the below error message.
Solution
Inside Configuration() method of Startup.cs file, we need to change the below code.
app.UseMvc(routes => {
routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
});
To
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});
The thing is after migration to ASP.NET Core 3.1, we have to use UseEndPoints() instead of UseMVC().
I hope this helps you resolve your issue after upgrading your project framework from ASP.NET Core 2.1 to ASP.NET Core 3.1.