简体   繁体   English

如何捕获异常并使用.NET Core中的状态代码进行响应

[英]How to catch an exception and respond with a status code in .NET Core

I am running a .Net Core Web API project. 我正在运行.Net Core Web API项目。 I have a startup file (below). 我有一个启动文件(如下)。 In the Startup.ConfigureServices(...) method, I add a factory method that creates an instance of IFoo. Startup.ConfigureServices(...)方法中,我添加了一个创建IFoo实例的工厂方法。 I want to catch any exceptions that the IFooFactory throws and return a better error message with a status code. 我想捕获IFooFactory抛出的任何异常,并返回带有状态代码的更好的错误消息。 At the moment I getting a 500 Error with the exception Message. 目前,我收到异常消息的500错误。 Can anyone help? 有人可以帮忙吗?

500错误消息

public interface IFooFactory
{
    IFoo Create();  
}

public class FooFactory : IFooFactory
{
    IFoo Create()
    {
        throw new Exception("Catch Me!");
    }
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IFooFactory,FooFactory>();
        services.AddScoped(serviceProvider => {
            IFooFactory fooFactory = serviceProvider.GetService<IFooFactory>();
            return fooFactory.Create(); // <== Throws Exception
        });
    }
}

So I've posted two different answers as I read the question in two different ways - lots of deleting/undeleting/editing - not sure which one actually answers your question: 因此,当我以两种不同的方式阅读问题时,我发布了两个不同的答案 - 大量删除/取消删除/编辑 - 不确定哪一个实际上回答了您的问题:

To figure out what is going wrong when the app starts and doesn't work at all try this: 要弄清楚应用程序启动时出现的问题并且根本不起作用,请尝试以下操作:

Use the developer exception page in Startup : 使用Startup的开发人员例外页面:

public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                           ILoggerFactory loggerFactory)
{
    app.UseDeveloperExceptionPage();
}

And in the Program class: Program类中:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .CaptureStartupErrors(true) // useful for debugging
        .UseSetting("detailedErrors", "true") // what it says on the tin
        .Build();

    host.Run();
}

If you want to handle an occasional exception when the api is generally working then you can use some middleware: 如果你想在api正常工作时偶尔处理异常,那么你可以使用一些中间件:

public class ExceptionsMiddleware
{
    private readonly RequestDelegate _next;

    /// <summary>
    /// Handles exceptions
    /// </summary>
    /// <param name="next">The next piece of middleware after this one</param>
    public ExceptionsMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    /// <summary>
    /// The method to run in the piepline
    /// </summary>
    /// <param name="context">The current context</param>
    /// <returns>As task which is running the action</returns>
    public async Task Invoke(HttpContext context)
    {
        try
        {
            await _next.Invoke(context);
        }
        catch(Exception ex)
        {
            // Apply some logic based on the exception
            // Maybe log it as well - you can use DI in
            // the constructor to inject a logging service

            context.Response.StatusCode = //Your choice of code
            await context.Response.WriteAsync("Your message");
        }
    }
}

There is one 'gotcha' with this - You can't write the status code if the response header has already been sent. 这有一个“问题” - 如果已经发送了响应头,则无法编写状态代码。

You configure the middleware in the Startup class using the Configure method: 您可以使用Configure方法在Startup类中配置中间件:

public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                           ILoggerFactory loggerFactory)
{
    app.UseMiddleware<ExceptionsMiddleware>();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM