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中获得controller、url及action的asp.net方法详解

解决win7安装visual studio 2015失败的方法

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

asp.net图形验证码生成实践

介绍oom中automapper的使用方法

asp.net》数据的绑定―repeater图文详解

asp.net mvc 对输入的字符串字段做trim处理的方法_实用技巧

asp.net core类库项目中如何实现读取配置文件的详解

理解asp.net中多层架构

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




打赏

取消

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

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

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

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

评论

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