ASP.NET Core异常和错误处理(8)_实用技巧


本文摘自PHP中文网,作者微波,侵删。

这篇文章主要为大家详细介绍了ASP.NET Core异常和错误处理的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在这一章,我们将讨论异常和错误处理。当 ASP.NET Core应用程序中发生错误时,您可以以各种不同的方式来处理。让我们来看看通过添加一个中间件来处理异常情况,这个中间件将帮助我们处理错误。

要模拟出错,让我们转到应用程序,运行,如果我们只是抛出异常的话,看看程序是如何运转转的。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

using Microsoft.AspNet.Builder;

using Microsoft.AspNet.Hosting;

using Microsoft.AspNet.Http;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Configuration;

namespace FirstAppDemo {

 public class Startup {

  public Startup() {

   var builder = new ConfigurationBuilder()

   .AddJsonFile("AppSettings.json");

   Configuration = builder.Build();

  }

  public IConfiguration Configuration { get; set; }

    

  // This method gets called by the runtime.

  // Use this method to add services to the container.

  // For more information on how to configure your application,

  // visit http://go.microsoft.com/fwlink/?LinkID=398940

  public void ConfigureServices(IServiceCollection services) {

  }

   

  // This method gets called by the runtime.

  // Use this method to configure the HTTP request pipeline.

  public void Configure(IApplicationBuilder app) {

   app.UseIISPlatformHandler();

   app.UseRuntimeInfoPage();

    

   app.Run(async (context) => {

   throw new System.Exception("Throw Exception");

   var msg = Configuration["message"];

   await context.Response.WriteAsync(msg);

   });

  }

    

  // Entry point for the application.

  public static void Main(string[] args) => WebApplication.Run<Startup>(args);

 }

}

它只会抛出一个非常通用的异常信息。保存Startup.cs页面并运行您的应用程序。

阅读剩余部分

相关阅读 >>

asp.net mvc 4 中的json数据交互的方法

分析asp.net 2.0 session 丢失的几种情况

使用action的模型绑定实例教程

介绍asp.net使用session的方法

asp.net部署到iis常见问题的解决方法_实用技巧

linux下搭建.net core环境方法步骤

关于asp.net core网站在docker中运行的详解

asp.net中有关config文件的读写功能讲解

asp.net中core优雅的在开发环境保存机密(user secrets)的详解

asp.net页脚制作详解

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...