【问题标题】:how to create a request when an exception is caught in ASP.NET Core app在 ASP.NET Core 应用程序中捕获异常时如何创建请求
【发布时间】:2021-06-24 19:09:52
【问题描述】:

我想在我的代码中发现异常后,将用户重定向到我自己设计的错误页面。截至目前,应用程序吃掉了异常并继续运行。

我有一个 ErrorController.cs 包含:

public class ErrorController : Controller
{
    public IActionResult MyError()
    {
        return View();
    }
}

还有一个错误页面MyError.cshtml:

@{
    ViewData["Title"] = "MyError";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

<h3>Development Mode</h3>
<p>
    Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
    <strong>Development environment should not be enabled in deployed applications</strong>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>, and restarting the application.
</p>

我的问题是我不知道如何让我用来查看我的应用程序的浏览器重定向到这个错误页面mysite.com/Error/MyError

据我了解,我需要向/Error/MyError 发出请求,然后控制器将打开.cshtml 页面。如果我在程序的其他地方(不是在控制器中)捕获异常,我需要这样做。

这方面的最佳做法是什么,或者老实说,有什么方法可以做到这一点?

注意:我正在寻找一个解决方案,以防引发的异常没有具有 HTTP 状态代码。抛出的异常之一是Microsoft.Data.SqlClient.SqlException 我已经看到一些关于如何使用web.config 为特定状态代码显示自定义错误页面的文档,但我认为这在这种情况下不会起作用。 (如有错误请指正)

【问题讨论】:

  • 附带说明一下,我一般是编码新手,并且很乐意尽我所能澄清这个问题中任何不清楚的地方。

标签: c# asp.net-core exception


【解决方案1】:

来自 ASP.NET 停靠点 hereStartup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/error");
    }

    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

[ApiController]
public class ErrorController : ControllerBase
{
    [Route("/error")]
    public IActionResult Error() => Problem();
}

没试过,但我不明白为什么

[ApiController]
public class ErrorController : ControllerBase
{
    [Route("/error")]
    public IActionResult Error() => View();
}

没有用。

【讨论】:

    【解决方案2】:

    我认为您需要在 try catch 子句的 catch 中提供重定向。即

        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";
    
            try
            {
                throw new Exception(); // Do something here that can throw an Exception, but I have purposely thrown an Exception here so that it jumps into the catch
    
                return View();
            }
            catch (Exception ex)
            {
                return RedirectToAction("Error", "MyError");
            }
        }
    

    【讨论】:

    • (致其他 SO 成员 - 随时修改我的答案以反映最佳实践/错误使用 catch general Exception
    • 在这种情况下,异常完全在控制器之外抛出。我想知道如何从单独的文件中调用方法MyError()
    • 在控制器外的哪个区域出现异常?在像Startup.cs这样更高的地方?
    • 是的,是startup.cs文件出错
    猜你喜欢
    • 2017-04-24
    • 2019-05-29
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2011-10-13
    • 2018-10-28
    相关资源
    最近更新 更多