I am trying to implement a simple custom error handling page, after went through different approaches online, I have the following code:
Statup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler("/Error");
app.UseStaticFiles();
app.UseAuthentication();
app.UseStatusCodePagesWithRedirects("/Error/StatusCode/{0}");
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
ErrorController.cs
public class ErrorController : Controller
{
private IHttpContextAccessor _context;
public ErrorController(IHttpContextAccessor context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
public IActionResult Error()
{
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
var exception = context.Error;
var statusCode = HttpContext.Response.StatusCode;
var message = context.Error.Message;
var stackTrace = context.Error.StackTrace;
return View(new ErrorViewModel { StatusCode = statusCode });
}
[AllowAnonymous]
public IActionResult StatusCode(int? Id)
{
//var exceptionDetails = _context.HttpContext.Features.Get<IExceptionHandlerPathFeature>();
if (Id.HasValue && Id != null)
{
if (Id == 404 || Id == 500 || Id == 403)
{
var viewName = Id.ToString();
return View("StatusCode", new ErrorViewModel { ErrorCode = Id.ToString() });
}
}
return View();
}
}
When I trigger an exception e.g. accessing http://localhost/Error/Error, which invokes the Error action in the Error Controller, but it returnes null to the object:
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
thus I couldn't get any useful information I want from the page for troubleshooting later. Is there something I did wrong in this method?
If I trigger a HTTP 403 by accessing some resource the current user does not have acces to e.g. http://localhost/Requests/Details/123, it returns a view like:
return new ForbidResult();
which invokes the StatusCode action in Error Controller, but the only information I could get is the Id variable passed in which is the status code 403.
And if I try to access some resource doesn't exist e.g. http://localhost/Ghost, it will also go to the StatusCode action in Error Controller with a code 404.
It seems by using UseStatusCodePagesWithRedirects, it would fit better to what I want to achieve. But I am wondering is there anyway I could retrieve more application related information to return to the view or write into event log instead of just a genertic message with Http status code?
Thanks.
I am trying to implement a simple custom error handling page, after went through different approaches online, I have the following code:
Statup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler("/Error");
app.UseStaticFiles();
app.UseAuthentication();
app.UseStatusCodePagesWithRedirects("/Error/StatusCode/{0}");
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
ErrorController.cs
public class ErrorController : Controller
{
private IHttpContextAccessor _context;
public ErrorController(IHttpContextAccessor context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
public IActionResult Error()
{
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
var exception = context.Error;
var statusCode = HttpContext.Response.StatusCode;
var message = context.Error.Message;
var stackTrace = context.Error.StackTrace;
return View(new ErrorViewModel { StatusCode = statusCode });
}
[AllowAnonymous]
public IActionResult StatusCode(int? Id)
{
//var exceptionDetails = _context.HttpContext.Features.Get<IExceptionHandlerPathFeature>();
if (Id.HasValue && Id != null)
{
if (Id == 404 || Id == 500 || Id == 403)
{
var viewName = Id.ToString();
return View("StatusCode", new ErrorViewModel { ErrorCode = Id.ToString() });
}
}
return View();
}
}
When I trigger an exception e.g. accessing http://localhost/Error/Error, which invokes the Error action in the Error Controller, but it returnes null to the object:
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
thus I couldn't get any useful information I want from the page for troubleshooting later. Is there something I did wrong in this method?
If I trigger a HTTP 403 by accessing some resource the current user does not have acces to e.g. http://localhost/Requests/Details/123, it returns a view like:
return new ForbidResult();
which invokes the StatusCode action in Error Controller, but the only information I could get is the Id variable passed in which is the status code 403.
And if I try to access some resource doesn't exist e.g. http://localhost/Ghost, it will also go to the StatusCode action in Error Controller with a code 404.
It seems by using UseStatusCodePagesWithRedirects, it would fit better to what I want to achieve. But I am wondering is there anyway I could retrieve more application related information to return to the view or write into event log instead of just a genertic message with Http status code?
Thanks.
Share Improve this question asked Mar 5 at 19:14 Xiao HanXiao Han 1,0247 gold badges20 silver badges38 bronze badges1 Answer
Reset to default 0If you need more details (such as the original URL or query parameters), consider using UseStatusCodePagesWithReExecute
instead of UseStatusCodePagesWithRedirects
:
app.UseStatusCodePagesWithReExecute("/Error/StatusCode/{0}");
Then, in your error action, you can retrieve additional information using:
var statusCodeReExecuteFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
This feature provides properties like OriginalPath
and OriginalQueryString
, which can be useful for logging or for providing more context on your error page.