本文介绍了app.UseErrorHandler()可以访问错误详细信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC4应用程序中,我有一个Application_Error(object sender, EventArgs e)的global.asax.cs覆盖,可以在其中提取exceptionstatusCoderequestedUrl(用于处理404).这将被发送到我的控制器,并且错误页面对于404s和5xx有所不同(它们获得了堆栈跟踪).我没有看到如何使用UseErrorHandler()将相同的信息发送到我的Error动作.我在ASP.NET Core中使用了正确的方法吗?

In my MVC4 app I had a global.asax.cs override of Application_Error(object sender, EventArgs e) where I could extract the exception, statusCode and requestedUrl (for handling 404). That would get sent to my controller and the error page would be different for 404s vs 5xx (these get a stack trace). I don't see how to get this same info to my Error action using UseErrorHandler(). Am I using the right approach in ASP.NET Core?

推荐答案

八月. 2016年2月2日-更新1.0.0

Startup.cs

using Microsoft.AspNet.Builder;

namespace NS
{
    public class Startup
    {
         ...
         public virtual void Configure(IApplicationBuilder app)
         {
             ...
             app.UseExceptionHandler("/Home/Error");
             ...
         }
     }
}

HomeController.cs

using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging;

namespace NS.Controllers
{
    public class HomeController : Controller
    {
        static ILogger _logger;
        public HomeController(ILoggerFactory factory)
        {
            if (_logger == null)
                _logger = factory.Create("Unhandled Error");
        }

        public IActionResult Error()
        {
            var feature = HttpContext.Features.Get<IExceptionHandlerFeature>();
            var error = feature?.Error;
            _logger.LogError("Oops!", error);
            return View("~/Views/Shared/Error.cshtml", error);
        }
    }
}

project.json

...
"dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0",
     ...
}
...

这篇关于app.UseErrorHandler()可以访问错误详细信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:09