Asp.net Core UseExceptionHandler无法使用POST请求

问题描述 投票:2回答:2

在我的.Net Core 2.0应用程序中,我已实现UseExceptionHandler以全局处理异常。但是重定向仅适用于GET方法,而POST方法始终返回状态码:404;找不到

这是我的Startup.cs配置方法

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
ILoggerFactory loggerFactory)
{
        app.UseCors(builder => builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseStatusCodePages();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseExceptionHandler("/api/v1/Error");
        }
        else
        {
            app.UseExceptionHandler("/api/v1/Error");
        }

        app.UseMvc();
        app.UseSwagger();

        app.UseSwaggerUI(c =>
        {
            c.RoutePrefix = "docs";
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
            c.SwaggerEndpoint("/[]virtualDir]/swagger/v1/swagger.json", "V1 Docs");
        });
    }

并且错误控制器是

[Route("api/v1/[controller]")]
public class ErrorController : Controller
{
    private readonly IErrorLoggerService _errorLoggerService;

    public ErrorController(IErrorLoggerService errorLoggerService)
    {
        _errorLoggerService= errorLoggerService;
    }
    [HttpGet]
    public IActionResult Get()
    {
        // Get the details of the exception that occurred
        var exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
        var errorLog = new ErrorLog();

        if (exceptionFeature != null)
        {
            // Get which route the exception occurred at
            var routeWhereExceptionOccurred = exceptionFeature.Path;

            // Get the exception that occurred
            var exceptionThatOccurred = exceptionFeature.Error;

            // TODO: save exception in db
            errorLog = new ErrorLog
            {
                Application = routeWhereExceptionOccurred,
                Source = exceptionThatOccurred.Source,
                CreatedDate = DateTime.Now,
                Host = exceptionThatOccurred.InnerException?.ToString(),
                Type = exceptionThatOccurred.Message,
                Detail = exceptionThatOccurred.StackTrace
            };
            _errorLoggerService.Save(errorLog);
            return Json(errorLog);
        }

        return Json(errorLog);
    }
}

如何解决?

c# exception asp.net-core-mvc asp.net-core-2.0
2个回答
2
投票

根据documentation

在MVC应用中,请勿使用HTTP方法属性(例如HttpGet)来修饰错误处理程序操作方法。显式动词阻止某些请求到达该方法。允许匿名访问该方法,以便未经身份验证的用户可以接收错误视图。

所以,您的错误是[HttpGet]属性中的[[可能。不要使用它,而是遵循文档:

[Route("")] [AllowAnonymous] public IActionResult Get() { .... }

0
投票
您应该能够从Error方法中删除[HttpGet],因为它是造成不必要限制的原因。

//[HttpGet] public IActionResult Get() { .... }

© www.soinside.com 2019 - 2024. All rights reserved.