asp.net-core-mvc - ASP 网络核心 3.1。UseExceptionHandler 不处理异常
问题描述
我想使用 UseExceptionHandle 全局处理异常并将用户重定向到生产环境中的自定义错误页面。我发现在调用异步控制器动作方法时,重定向以可预测的方式发生。但是同步操作调用会导致抛出异常并停止应用程序。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseStatusCodePagesWithReExecute("/Error/{0}");
app.UseHsts();
}
app.UseStatusCodePages();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
为了测试,我在同步和异步操作中抛出异常。
public async Task<ViewResult> Index(string user)
{
throw new Exception("My Custom Exception");
}
public ViewResult Index(string user)
{
throw new Exception("My Custom Exception");
}
这种行为的原因可能是什么?
解决方案
推荐阅读
- mysql - autocommit=true 时无法调用 commit
- c++11 - 如何在不使用循环的情况下获取 std::array 中项目的索引?
- python - 将图层连接到自身
- python - Django - 条纹。您没有设置有效的可发布密钥。尝试在 django 网站上合并条带付款时出现的错误
- postgresql - 在 PostgreSQL 中有条件地返回 SETOF
- c - 为什么我的代码在通过凯撒密码转换字母时会跳过空格和标点符号?
- ios - 如何让 UIButtons 跟随静态图像的大小调整?
- http - Autoit:HTTP 请求,如何登录网站
- javascript - jqueryUI,如果“drop to”容器满足要求,则接受
- django - 如何在 Django 社交登录上创建用户限制列表