首页新闻找找看学习计划

ASP.NET Core 2.2 迁移至 3.0 的 UseMvcWithDefaultRoute 问题

0
悬赏园豆:30 [已解决问题] 解决于 2019-05-16 21:23

之前基于 ASP.NET Core 2.2 的代码如下

public static IApplicationBuilder UseCustomErrorPages(this IApplicationBuilder app)
{
    var options = new ExceptionHandlerOptions
    {
        ExceptionHandlingPath = "/errors/500",
        ExceptionHandler = app.New().UseMvcWithDefaultRoute().Build()
    };
    app.UseExceptionHandler(options);
    app.UseStatusCodePagesWithReExecute("/errors/{0}");
    return app;
}

迁移到 ASP.NET Core 3.0 之后运行报错

Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(...).
   at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app, Action`1 configureRoutes)
   at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvcWithDefaultRoute(IApplicationBuilder app)

请问如何解决?

dudu的主页 dudu | 高人七级 | 园豆:41254
提问于:2019-05-15 14:24
< >
分享
最佳答案
0

将 UseMvcWithDefaultRoute() 改为 UseRouting() + UseEndpoints() 就可以了

public static IApplicationBuilder UseCustomErrorPages(this IApplicationBuilder app)
{
    var options = new ExceptionHandlerOptions
    {
        ExceptionHandlingPath = "/errors/500",
        ExceptionHandler = app.New()
            .UseRouting()
            .UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute())
            .Build()
    };
    app.UseExceptionHandler(options);
    app.UseStatusCodePagesWithReExecute("/errors/{0}");
    return app;
}
dudu | 高人七级 |园豆:41254 | 2019-05-16 21:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册