azure - 如何在适用于 Linux 的 azure web 应用程序中自定义默认错误页面?

标签 azure azure-web-app-service azure-app-service-plans

我已在适用于 Linux 的 Azure Web 应用程序中托管了 .NET core 3.1.1 LTS 应用程序?如何自定义默认错误页面,例如 1.502 2 应用服务停止时的错误页面。 3 从 Visual studio、VS code/FTP 发布应用程序时出现错误页面

最佳答案

我找到了有关创建应用程序网关自定义错误页面的文档,也许它对您有好处。 custom error pages 如何在代码中创建自定义错误页面。

我们也可以通过代码来处理它。这是我的建议。

像500这样的错误,我们可以通过过滤器来处理

public class ErrorPageFilter : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext context)
    {
        if (context.HttpContext.Response.StatusCode == 500)
        {
            context.HttpContext.Response.Redirect("error/500");
        }
        base.OnResultExecuted(context);
        }
}

[ErrorPageFilter]
public abstract class PageController
{}

对于.netcore mvc项目,它在创建时还附带了对错误页面的管道处理。

在startup.cs中,

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
        //dev env show error page
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            //prod env show custom page
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }
}

//In HomeController has this function,
//You just replace cshtml file by you want
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore =     true)]
public IActionResult Error()
{
    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ??     HttpContext.TraceIdentifier });
}

像 404 这样的错误,我们可以

在startup.cs文件中找到configure函数,然后添加以下代码,

  app.UseStatusCodePagesWithReExecute("/error/{0}");

然后添加错误 Controller ,

public class ErrorController : Controller
{
        /// <summary>
        /// In content in {0} is error code
        /// </summary>
        /// <returns></returns>
        [Route("/error/{0}")]
        public IActionResult Page()
        {
            //Jump to 404 error page
            if (Response.StatusCode == 404)
            {
                return View("/views/error/notfound.cshtml");
            }
            return View();
        }
}

注意,如果使用ErroeController(处理404错误),请不要使用 app.UseExceptionHandler("/Home/Error"), 你只需要在 Controller 中处理错误。

关于azure - 如何在适用于 Linux 的 azure web 应用程序中自定义默认错误页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60784731/

相关文章:

azure devops yaml管道未设置变量

azure - 无法更新自动缩放配置

linux - 如何在 Linux 中使用 Azure 应用服务中的自定义 DNS 服务器?

azure - 来自 Azure Web App 的一个实例的 HTTP 502

Azure应用服务: How can I determine which process is consuming high CPU?

azure - Webjobs 方法索引 - 依赖注入(inject)异常 IConfiguration

c# - 机器人框架 : Why can't I use SendAsync?

android - Azure禁用推送通知android

azure - 如何在不使用公共(public) DNS 的情况下测试需要自定义域的 Azure Web App

asp.net - 如何将包含两个项目的解决方案部署到Azure App Service?