Asp.Net Core-异常处置惩罚
Asp.Net Core-异常处置惩罚
在这一章,我们将议论异常和毛病处置惩罚。当 ASP.NET Core应用递次中发作毛病时,您可以以种种差别的体式格局来处置惩罚。让我们来看看经由过程增加一个中心件来处置惩罚异常情况,这个中心件将协助我们处置惩罚毛病。
要模仿失足,让我们转到应用递次,运转,假如我们只是抛出异常的话,看看递次是怎样运转转的。
1 using Microsoft.AspNet.Builder; 2 using Microsoft.AspNet.Hosting; 3 using Microsoft.AspNet.Http; 4 using Microsoft.Extensions.DependencyInjection; 5 using Microsoft.Extensions.Configuration; 6 namespace FirstAppDemo { 7 public class Startup { 8 public Startup() { 9 var builder = new ConfigurationBuilder() 10 .AddJsonFile("AppSettings.json"); 11 Configuration = builder.Build(); 12 } 13 public IConfiguration Configuration { get; set; } 14 15 // This method gets called by the runtime. 16 // Use this method to add services to the container. 17 // For more information on how to configure your application, 18 // visit http://go.microsoft.com/fwlink/?LinkID=398940 19 public void ConfigureServices(IServiceCollection services) { 20 } 21 22 // This method gets called by the runtime. 23 // Use this method to configure the HTTP request pipeline.24 public void Configure(IApplicationBuilder app) { 25 app.UseIISPlatformHandler(); 26 app.UseRuntimeInfoPage(); 27 28 app.Run(async (context) => { 29 throw new System.Exception("Throw Exception"); 30 var msg = Configuration["message"]; 31 await context.Response.WriteAsync(msg); 32 }); 33 } 34 35 // Entry point for the application. 36 public static void Main(string[] args) => WebApplication.Run<Startup>(args); 37 } 38 }
它只会抛出一个异常通用的异常信息。保留Startup.cs页面并运转您的应用递次。
您将看到我们未能加载此资本。涌现了一个 HTTP 500 毛病,内部服务器毛病,谁人页面不是很有协助。它可以很轻易获得一些异常信息。
让我们增加另一个中心件 UseDeveloperExceptionPage。
1 // This method gets called by the runtime. 2 // Use this method to configure the HTTP request pipeline. 3 public void Configure(IApplicationBuilder app) { 4 app.UseIISPlatformHandler(); 5 app.UseDeveloperExceptionPage(); 6 app.UseRuntimeInfoPage(); 7 8 app.Run(async (context) => { 9 throw new System.Exception("Throw Exception"); 10 var msg = Configuration["message"]; 11 await context.Response.WriteAsync(msg); 12 }); 13 }14
这个中心件与其他的有点差别,其他中心件一般监听传入的要求并对要求做一些相应。
UseDeveloperExceptionPage不会云云在乎传入的要求在以后的管道会发作什么。
它只是挪用下一个中心件,然后再守候,看看管道中是不是会涌现异常,假如有异常,这块中心件会给你一个关于该异常的毛病页面。
如今让我们再次运转应用递次。将会发生一个以下面的屏幕截图所示的输出。
如今假如递次中涌现异常,您将在页面中看到一些想要看到的异常信息。你也会获得一个客栈跟踪:这里可以看到Startup.cs第37行有一个未处置惩罚的异常抛出。
所有这些异常信息对开发人员将异常有效。事实上,我们可以只愿望当开发人员运转应用递次时才显现这些异常信息。
Asp.Net Core 静态文件
Asp.Net Core 静态文件
案例
在这一章,我们将进修怎样运用文件。险些每一个web应用递次都须要一个主要特征:可以从文件体系供应文件(静态文件)。
静态文件像JavaScript文件、图片、CSS文件等,我们Asp.Net Core应用递次可以直接供应给客户。
静态文件一般位于web根(wwwroot)文件夹。
默许情况下,这是我们可以直接从文件体系供应文件的唯一的处所。
案例
如今让我们经由过程一个简朴的示例来相识我们在我们的应用递次怎样供应这些静态文件。
在这里,我们想要向我们的 FirstAppDemo 应用递次增加一个简朴的 HTML 文件,该 HTML 文件放在web 根 (wwwroot) 文件夹。在处理方案资本治理器中右键单击wwwroot文件夹并挑选Add→新项。
在中心窗格中,挑选 HTML 页面并称之为 index.html,单击增加按钮。
你会看到一个简朴的index.html文件。让我们在个中增加一些简朴的文本和题目以下所示。
1 2 3 4 5 6 7 8 9 10 |
|
当您运转应用递次并在浏览器中输入index.html时,您将看到app.Run中心件将抛出一个异常,由于现在在我们的应用递次中什么都没有。
如今我们的项目中没有中心件会去找文件体系上的任何文件。
为相识决这个题目,经由过程在处理方案资本治理器中右键单击您的项目并挑选治理NuGet包进入到NuGet包治理器。
搜刮 Microsoft.AspNet.StaticFiles,会找到静态文件中心件。让我们装置此 nuget 递次包,如今我们可以在Configure要领中注册中心件。
让我们鄙人面的递次中所示的Configure要领中增加 UseStaticFiles 中心件。
1 using Microsoft.AspNet.Builder; 2 using Microsoft.AspNet.Hosting; 3 using Microsoft.AspNet.Http; 4 using Microsoft.Extensions.DependencyInjection; 5 using Microsoft.Extensions.Configuration; 6 namespace FirstAppDemo { 7 public class Startup { 8 public Startup() { 9 var builder = new ConfigurationBuilder() 10 .AddJsonFile("AppSettings.json"); 11 Configuration = builder.Build(); 12 } 13 public IConfiguration Configuration { get; set; } 14 15 // This method gets called by the runtime. 16 // Use this method to add services to the container. 17 // For more information on how to configure your application, 18 // visit http://go.microsoft.com/fwlink/?LinkID=398940 19 public void ConfigureServices(IServiceCollection services) { 20 } 21 22 // This method gets called by the runtime. 23 // Use this method to configure the HTTP request pipeline. 24 public void Configure(IApplicationBuilder app) { 25 app.UseIISPlatformHandler(); 26 app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); 27 app.UseStaticFiles(); 28 29 app.Run(async (context) => { 30 throw new System.Exception("Throw Exception"); 31 var msg = Configuration["message"]; 32 await context.Response.WriteAsync(msg); 33 }); 34 } 35 36 // Entry point for the application. 37 public static void Main(string[] args) => WebApplication.Run<Startup>(args); 38 } 39 }
除非你经由过程传入一些差别的设置参数来掩盖选项,不然静态文件会关于一个给定的要求看做是要求途径。这个要求途径是相干于文件体系。
假如静态文件依据url找到一个文件,它将直接返回该文件,而不挪用下一个块中心件。
假如没有找到婚配的文件,那末它会继承实行下一个块中心件。
让我们保留Startup.cs文件并革新浏览器。
你如今可以看到index.html文件。你安排在wwwroot文件夹下任何处所的任何JavaScript文件、CSS文件或许HTML文件,您都可以在Asp.Net Core中直接当静态文件运用。
在假如你想 让index.html作为您的默许文件,IIS一向有这类功用。
你可以给 IIS 一个默许文件列表。假如有人接见根目录,在这类情况下,假如 IIS 找到命名为 index.html的文件,它就会自动将该文件返回给客户端。
让我们如今最先举行少许变动。起首,我们须要删除强迫的毛病,然后增加另一块的中心件,这就是 UseDefaultFiles。以下是设置要领的完成。
1 / This method gets called by the runtime. 2 // Use this method to configure the HTTP request pipeline. 3 public void Configure(IApplicationBuilder app) { 4 app.UseIISPlatformHandler(); 5 app.UseDeveloperExceptionPage(); 6 7 app.UseRuntimeInfoPage(); 8 app.UseDefaultFiles(); 9 app.UseStaticFiles(); 10 11 app.Run(async (context) => { 12 var msg = Configuration["message"]; 13 await context.Response.WriteAsync(msg); 14 }); 15 }
这段中心件将监听传入的要求,假如要求是根目录,就检察是不是有婚配的默许文件。
您可以掩盖这个中心件的选项来通知它怎样婚配默许文件,但index.html是默许情况下的一个默许的文件。
让我们保留 Startup.cs 文件并将您的浏览器转到 web 应用递次的根目录。
你如今可以看到index.html是默许文件。你装置中心件的递次是很主要的,由于假如你将UseDefaultFiles安排在UseStaticFiles以后,你将可以不会获得雷同的效果。
假如你想要运用UseDefaultFiles和UseStaticFiles中心件,你可以运用另一个中心件Microsoft.aspnet.staticfiles,它也是NuGet包,它是一个服务器中心件。这本质上是以准确的递次包含了默许文件和静态文件。
1 // This method gets called by the runtime. 2 // Use this method to configure the HTTP request pipeline. 3 public void Configure(IApplicationBuilder app) { 4 app.UseIISPlatformHandler(); 5 app.UseDeveloperExceptionPage(); 6 7 app.UseRuntimeInfoPage(); 8 app. UseFileServer(); 9 10 app.Run(async (context) => { 11 var msg = Configuration["message"]; 12 await context.Response.WriteAsync(msg); 13 }); 14 }
让我们再一次保留 Startup.cs 文件。一旦你革新浏览器,你将看到雷同的效果,以下面的屏幕快照所示。
以上就是asp.net core实例教程之异常处置惩罚与静态文件教程的细致内容,更多请关注ki4网别的相干文章!