一、新配置路由策略

在 Asp.Net Core 3.0中默认不再支持app.UserMvc() 方式配置路由 系统。 而是使用新的模式,点击查看asp.netCore3.0区域和路由配置变化 默认使用的话 会抛出异常:
InvalidOperationException: Endpoint Routing does not support ‘IApplicationBuilder.UseMvc(…)’.
To use ‘IApplicationBuilder.UseMvc’ set ‘MvcOptions.EnableEndpointRouting = false’ inside ‘ConfigureServices(…).

.NET Core | Core3.0 中使用app.UseMvc() - 图1

二、Asp.Net Core 3.0中 对于app.UseMvc() 还是 支持的。

如果你 习惯了以前配置方式,还是可以使用的。 进需要在 配置服务 中 ConfigureServices 增加mvc支持,并且关闭终点路由。 EnableEndpointRouting=false . 示例代码如下:
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. //配置Mvc + json 序列化
  4. services.AddMvc(options => { options.EnableEndpointRouting = false; })
  5. .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
  6. .AddNewtonsoftJson(options =>
  7. {
  8. options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
  9. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm";
  10. });
  11. }
  12. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  13. {
  14. if (env.IsDevelopment())
  15. {
  16. app.UseDeveloperExceptionPage();
  17. }
  18. app.UseStaticFiles();
  19. app.UseAuthorization();
  20. app.UseMvc(routes =>
  21. {
  22. routes.MapRoute(
  23. name: "default",
  24. template: "{controller=Home}/{action=Index}/{id?}");
  25. });
  26. //app.UseRouting();
  27. //app.UseEndpoints(endpoints =>
  28. //{
  29. // endpoints.MapControllers();
  30. //});
  31. }
更多:

asp.netCore3.0区域和路由配置变化

在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 库序列化数据

Asp.Net Core Cookie使用,Asp.net Core Cookie操作失效