简体   繁体   English

.Net Core 2.2启动中的Services.Configuration.Replace

[英]Services.Configuration.Replace in .Net Core 2.2 Startup

I am looking for option to override default 404 not found Route error in Web API and found this link Override Route 我正在寻找替代默认API 404的选项,但未在Web API中找到路由错误,并且发现此链接覆盖路由

As per the description, I could create class like below 根据描述,我可以创建如下的类

   public class HttpNotFoundAwareController:ApiControllerActionSelector
{

    public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
    {
        HttpActionDescriptor descriptor = null;
        try
        { 
        descriptor= base.SelectAction(controllerContext);
        }
        catch(HttpResponseException ex)
        {
            var code = ex.Response.StatusCode;
        }

        return descriptor;
    }
}

However, this overriding needs to also be registered. 但是,此替代也需要注册。 From the link it shall be done in WebAPIConfig as below 通过链接,应在WebAPIConfig中完成,如下所示

configuration.Services.Replace(typeof(IHttpControllerSelector), new HttpNotFoundAwareDefaultHttpControllerSelector(configuration));

However I am not sure, how I could register the overriding in .Net Core Startup.cs file? 但是我不确定,如何在.Net Core Startup.cs文件中注册覆盖? as there is no Replace attribute available for Services. 因为没有“服务”可用的“替换”属性。

Can anybody help? 有人可以帮忙吗?

For capturing 404 and return custom error, you could try UseStatusCodePages like 为了捕获404并返回自定义错误,您可以尝试使用UseStatusCodePages

app.UseStatusCodePages(new StatusCodePagesOptions {
    HandleAsync = async context =>
    {
        if (context.HttpContext.Response.StatusCode == StatusCodes.Status404NotFound)
        {
            await context.HttpContext.Response.WriteAsync($"{context.HttpContext.Request.Path} is not Found");
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启动中的 ASP.NET 内核配置部分 - ASP.NET Core Configuration Section in Startup 如何在.net core 2.2中的启动文件中注册polly? - How to register polly in startup file in .net core 2.2? .NET Core 2.2下配置中的AddJsonFile选项的默认设置是什么? - What is the default for AddJsonFile options in configuration under .NET Core 2.2? Startup.cs 从 .net 内核 2.2 迁移到 .net 内核 3.1 - Startup.cs migration from .net core 2.2 to .net core 3.1 Serilog无法使用asp.net core 2.2 API中的配置 - Serilog not working from configuration in asp.net core 2.2 API .NET Core 2.2 - 无法从 Azure AppConfig 获取应用程序配置 - .NET Core 2.2 - Unable to get app configuration from Azure AppConfig .Net core web api 在调用 Startup 之前配置服务 - .Net core web api configure services before Startup is called .NET Core 2.0分离Startup.cs服务注入 - .NET Core 2.0 Separating Startup.cs Services Injection 在startup.cs中注入所有服务,.net核心,重载 - inject all services in startup.cs, .net core, overloaded 在迁移到 .Net Core 3.1 时,.Net Core 2.2 中使用的 Services.AddMvc() 和 SuperJsonOutputFormatter 的替代方法是什么 - what is the alternative for Services.AddMvc() and SuperJsonOutputFormatter used in .Net Core 2.2 while migrating to .Net Core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM