asp.net-core - app.UseStatusCodePagesWithReExecute("/Error/{0}"); 在 ASP.NET Core 2.2 中不工作
问题描述
我把它放在 Startup.cs 的 Configure 方法中
app.UseMvc();
app.UseStatusCodePagesWithReExecute("/Error/{0}");
然后我创建了一个这样的控制器:
[Route("Error/{statusCode}")]
public IActionResult HandleErrorCode(int statusCode)
{
var statusCodeData = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
switch (statusCode)
{
case 404:
ViewBag.ErrorMessage = "Sorry the page you requested could not be found";
ViewBag.RouteOfException = statusCodeData.OriginalPath;
break;
case 500:
ViewBag.ErrorMessage = "Sorry something went wrong on the server";
ViewBag.RouteOfException = statusCodeData.OriginalPath;
break;
}
return View();
}
当我导航到 localhost:port/doesnt-exist
叹息......我得到一个空白页。
解决方案
空白页的原因是没有找到视图。因为我不再使用开发人员异常页面,所以我无法轻易意识到这一点。
I used
View("~/Views/Shared/Error.cshtml")
代替
View();
推荐阅读
- python - 如何改进我的循环 Python 脚本,在每个循环中针对不同条件涉及不同的数学运算?
- azure - Dotnet Core:如何从外部库获取配置
- python - Python 中的行数
- android - 如何从xml中删除节点?
- javascript - Number()、parseInt()、String() 等 Javascript 函数在哪里定义?
- xamarin - 如何在skiasharp xamarin表单中调用PaintSurface两次
- c# - 仅在文本框上的路径
- python - 从文件中简单解析和排序数据
- firebase - 从 Fire 基础服务器时间戳获取日期并将其显示在 ionic 3 应用程序上
- logging - 如何写入 VSTS 日志输出?