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;
}
DotNETCore 学习笔记 异常处理的更多相关文章
- Python 2.7 学习笔记 异常处理
如同别的开发语言,python也支持异常处理机制.本文介绍下它的基本语法. 一.异常的基本处理框架如下: try: 业务代码 except 异常类1: 异常处理代码 except 异常类2: 异常处理 ...
- python基础学习笔记——异常处理
异常处理流程图 一,异常和错误 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法 ...
- C++学习笔记-异常处理
程序设计的要求之一就是程序的健壮性.希望程序在运行时能够不出或者少出问题.但是,在程序的实际运行时,总会有一些因素会导致程序不能正常运行.异常处理(Exception Handling)就是要提出或者 ...
- Python3学习笔记——异常处理
#!/usr/bin/env python # 1.异常处理 try: # 主要执行的代码 except IndexError as e: # 对于某些错误需要特殊处理的,可以对特殊错误进行捕捉 pr ...
- JavaScript入门学习笔记(异常处理)
try:语句测试代码块的错误,当try中的代码块出错时执行catch中的代码块. catch:语句处理错误: throw:语句创建或抛出自定义异常. 三者一起使用可以控制程序流并生成自定义异常信息. ...
- DotNETCore 学习笔记 WebApi
API Description Request body Response body GET /api/todo Get all to-do items None Array of to-do ite ...
- DotNETCore 学习笔记 MVC视图
Razor Syntax Reference Implicit Razor expressions <p>@DateTime.Now</p> <p>@DateTim ...
- DotNETCore 学习笔记 宿主
Hosting -------------------------------------------------------------------------- Setting up a Host ...
- DotNETCore 学习笔记 依赖注入和多环境
Dependency Injection ------------------------------------------------------------------------ ASP.NE ...
随机推荐
- 【WPF】 布局篇
[WPF] 布局篇 一. 几个常用且至关重要的属性 1. Width,Height : 设置窗体,控件宽高. 这里注意,WPF是自适应的, 所以把这2个属性设置 Auto, 则控件宽高会自动改变. 2 ...
- Word 2013 无法撤销操作的错误
来自 <http://delxu.blog.51cto.com/975660/1409139> 关闭正在运行的所有程序. 按Win-R,在运行框中键入regedit,然后单击“确定”. 在 ...
- 设置socket接收和发送超时的一种方式
Linux环境设置Socket接收和发送超时: 须如下定义:struct timeval timeout = {3,0}; //设置发送超时setsockopt(socket,SOL_SOCKET, ...
- ACE_Select_Reactor_T 介绍 (2)
本章目录 ACE_Select_Reactor_T 介绍 类继承图 类协作图 类主要成员变量 事件处理函数调用图 事件处理主流程 handle_events 函数流程 handle_events_i ...
- centos7安装python3.7
Centos7安装Python3的方法 由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_ ...
- 阿里云DTS VS MySQLdump
云平台的到来,使得越来越多用户的数据库由云下迁到云上.对于这种情况,阿里对此提出两种方案,一种是MySQL自带的MySQLdump,另外一种就是阿里云的DTS. DTS支持异构数据源之间的数据迁移同步 ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- HDU 1693 Eat the Trees(插头DP,入门题)
Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- Week1 Team Homework #1 from Z.XML-项目选择思路--基于对曾经大作业项目的思考
这两天试玩了一下去年学长的满分工程<shield star>游戏,再结合了一下他们团队的博客记录,有一种非常牛逼的感觉.具体对于这款游戏的一些思考和看法,毛大神已经说的很好了.因此,这里主 ...
- 使用ZSetOperations(有序)操作redis
方法 c参数 s说明 Boolean add(K key, V value, double score); K key:集合key V value:key对应的值 double score:分数 向 ...