将状态代码与消息重定向到错误页面。

huangapple go评论53阅读模式
英文:

Redirect StatusCode with message to Error Page

问题

最近发现了一个奇怪的行为。当我使用带有消息的状态代码时,无法重定向到默认错误消息。
我正在使用.Net Core(Net 7.0)

program.cs
将状态代码与消息重定向到错误页面。

HomeController.cs
将状态代码与消息重定向到错误页面。

输出
将状态代码与消息重定向到错误页面。

目标是重定向到这个页面
将状态代码与消息重定向到错误页面。

任何想法都将不胜感激。谢谢。

英文:

Recently found a strange behavior. I am not able to redirect to default error message when I use a status code with a message.
I am using .Net Core (Net 7.0)

program.cs
将状态代码与消息重定向到错误页面。

HomeController.cs
将状态代码与消息重定向到错误页面。

Output
将状态代码与消息重定向到错误页面。

The goal is a redirect to this page:
将状态代码与消息重定向到错误页面。

Any ideas will appreciated. Thank you.

答案1

得分: 1

我按照你提到的方式尝试了一下,发现如果返回带有错误消息的 ObjectResult,它不会触发错误处理程序,处理程序只处理 NotFoundResult 和 StatusCodeResult。

例如,如果我尝试使用 return StatusCode(500);,它可以命中 "/Home/Error",使用中间件 app.UseStatusCodePagesWithReExecute("/Home/Error");

尝试直接抛出带有消息的异常,然后在错误控制器中尝试以下方式:

var exceptionHandlerPathFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
var errmessage = exceptionHandlerPathFeature?.Error.Message;
var vm = new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrorMessage = errmessage };
return View(vm);

结果:

将状态代码与消息重定向到错误页面。

英文:

I tried as you mentioned and found that if you return an ObjectResult with ErrorMessage,it won't trigger the error handler,The handler only handle NotFoundResult\StatueCodeResult....

将状态代码与消息重定向到错误页面。
将状态代码与消息重定向到错误页面。

将状态代码与消息重定向到错误页面。

将状态代码与消息重定向到错误页面。

For example,if I try with return StatusCode(500); it could hit&quot;/Home/Error&quot; with the middleware app.UseStatusCodePagesWithReExecute(&quot;/Home/Error&quot;);

将状态代码与消息重定向到错误页面。

Try throw an Excption with message directly

and try as below in Error Controller:

var exceptionHandlerPathFeature =HttpContext.Features.Get&lt;IExceptionHandlerPathFeature&gt;();
            var errmessage = exceptionHandlerPathFeature?.Error.Message;
            var vm = new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrorMessage = errmessage };
            return View(vm);

Result:

将状态代码与消息重定向到错误页面。

答案2

得分: 0

UseExceptionHandler中间件用于捕捉异常,记录它并重新执行它,使用给定的路径。重新执行将会触发HomeController的Error方法,并从Views的共享文件夹返回Error视图。

根据您代码的第22行,您正在返回StatusCodeResult,这不是任何异常。使用UseExceptionHandler的思路是捕捉错误。

要在状态码上重定向到特定页面,您可以使用UseStatusCodePages中间件。

您可以执行以下操作:

  1. 添加中间件(在UseHttpRedirection之后添加)

  2. 在HomeController中添加相关的操作方法

  3. 在共享文件夹或主文件夹中添加视图。

英文:

UseExceptionHandler middleware is use to catch exception, log it and re-execute it with given path. Re-execution will hit the HomeController's Error method and it will return the Error view from shared folder of Views.

From your code on line no 22 you are returning StatusCodeResult and it is not any exception. Idea is UseExceptionHandler is use to catch error.

To redirect to particular page on status code, you can use UseStatusCodePages middleware.

You can do following:

  1. Add middleware (Add after UseHttpRedirection)

将状态代码与消息重定向到错误页面。

  1. Add related action method in HomeController

将状态代码与消息重定向到错误页面。

  1. Add views in shared or home folder.

将状态代码与消息重定向到错误页面。

huangapple
  • 本文由 发表于 2023年6月2日 03:08:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定