Created
September 15, 2017 09:22
-
-
Save codehaks/16a78967c5e01d2844fe817d4ce04d4a to your computer and use it in GitHub Desktop.
Simulating Error handling in ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace ErrorDemo { | |
public class Startup { | |
public void Configure (IApplicationBuilder app) { | |
app.UseDeveloperExceptionPage(); | |
app.UseExceptionHandler ("/error"); | |
app.Use (async (context, next) => { | |
if (context.Request.Path == "/throw") { | |
throw new Exception ("Something went wrong!"); | |
} | |
await context.Response.WriteAsync (Environment.NewLine + " No Error"); | |
await next.Invoke (); | |
}); | |
app.Run (async (context) => { | |
await context.Response.WriteAsync (Environment.NewLine + context.Request.Path); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment