【问题标题】:How can I make my error pages look more friendly?如何让我的错误页面看起来更友好?
【发布时间】:2022-01-06 15:49:42
【问题描述】:

我有一个 .Net Core + Entity Framework 应用程序,它允许用户查看引擎生产列表并编辑某些内容。

如果没有错误,一切看起来都很好。

但是当我遇到错误时,它会使用丑陋的视图而不是我的正常视图。

这是一个控制器示例,在没有错误时看起来很棒,但如果有错误,它只是白色背景上的黑色文本。

有没有办法让它适合我的网络应用程序的其余部分?

谢谢!

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,EngineName,FactoryName")] ProductionRound productionRound)
{
    if (ModelState.IsValid)
    {
        try
        {
            _context.Update(productionRound);
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!ProductionRoundExists(productionRound.Id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return RedirectToAction(nameof(Index));
    }
    return View(productionRound);
}

【问题讨论】:

    标签: asp.net-core entity-framework-core


    【解决方案1】:

    您可以自定义错误页面并使用UseStatusCodePagesWithReExecuteUseExceptionHandler 中间件来执行错误页面,如下所示:

    查看(StatusCode.cshtml 位于Views/Shared 文件夹中):

    <div class="alert alert-danger">
        <h1 class="alert-heading">
            <span>This is a user-friendly error page :)</span>
        </h1>
        Error code: @ViewData["code"]
    </div>
    

    控制器:

    public class ErrorController : Controller
    {
        [Route("Error/{statusCode}")]
        public IActionResult StatusCodeError(int statusCode)
        {
            ViewData["code"] = statusCode;
            return View("StatusCode");
        }
    }
    

    Startup.cs:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseStatusCodePagesWithReExecute("/Error/{0}");
        app.UseExceptionHandler("/Error/500");
    
        //other middleware...
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2016-12-29
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多