• DotNETCore 学习笔记 异常处理


    Error Handling
    
    public void Configure(IApplicationBuilder app, 
        IHostingEnvironment env)
    {
        app.UseIISPlatformHandler();
    
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
    
    
    public static void HomePage(IApplicationBuilder app)
    {
        app.Run(async (context) =>
        {
            if (context.Request.Query.ContainsKey("throw"))
            {
                throw new Exception("Exception triggered!");
            }
            var builder = new StringBuilder();
            builder.AppendLine("<html><body>Hello World!");
            builder.AppendLine("<ul>");
            builder.AppendLine("<li><a href="/?throw=true">Throw Exception</a></li>");
            builder.AppendLine("<li><a href="/missingpage">Missing Page</a></li>");
            builder.AppendLine("</ul>");
            builder.AppendLine("</body></html>");
    
            context.Response.ContentType = "text/html";
            await context.Response.WriteAsync(builder.ToString());
        });
    }
    
    
    app.UseExceptionHandler("/Error");
    [Route("/Error")]
    public IActionResult Index()
    {
        // Handle error here
    }
    
    Configuring Status Code Pages
    app.UseStatusCodePages();
    app.UseStatusCodePages(context =>
      context.HttpContext.Response.SendAsync("Handler, status code: " +
      context.HttpContext.Response.StatusCode, "text/plain"));
    
    app.UseStatusCodePages("text/plain", "Response, status code: {0}");
    
    app.UseStatusCodePagesWithRedirects("~/errors/{0}");
    app.UseStatusCodePagesWithReExecute("/errors/{0}");
    
    If you need to disable status code pages for certain requests, you can do so using the following code:
    var statusCodePagesFeature = context.Features.Get<IStatusCodePagesFeature>();
    if (statusCodePagesFeature != null)
    {
      statusCodePagesFeature.Enabled = false;
    }
  • 相关阅读:
    GCD
    Android仿人人客户端(v5.7.1)——对从服务器端(网络)获取的图片进行本地双缓存处理(编码实现)
    systimestamp
    byte数组之间的赋值,byte和TCHAR数组的赋值
    如何修改hotspot默认信道
    Wifi的密码长度有何限制
    微信心跳机制
    如何内置iperf到手机中
    如何adb shell进入ctia模式
    本周推荐10款免费的网站模板设计
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/5871216.html
Copyright © 2020-2023  润新知