gpt4 book ai didi

c# - ASP.NET MVC 5中发生任何错误时,重定向到 Controller 操作

转载 作者:行者123 更新时间:2023-12-03 08:28:51 26 4
gpt4 key购买 nike

发生任何错误时,我需要重定向到页面。
web.config

<customErrors mode="On" defaultRedirect="~/Error/Index">
</customErrors>
ErrorController.cs:
public class ErrorController : BaseController
{
// GET: Error
public ActionResult Index(string aspxerrorpath)
{
SendMail("Error Web", string.Format("Usuario: {0} .Error en: {1}", UserLogged == null ? "" : UserLogged.FullName(), aspxerrorpath));
return View("StringData","Se ha producido un error. Intentalo pasado unos minutos o envia un aviso al administrador desde el apartado de sugerencias");
}
}

enter image description here

最佳答案

在Global.asax.cs中添加以下内容的简单方法:

protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
// Log the exception.
logger.Error(exception);
Response.Clear();
Context.Response.Redirect("~/"); // it will redirect to just main page of your site. Replace this line to redirect whatever you need.
}

好的方法是将异常附加到HttpException,然后按照如下方式处理该异常:
HttpException httpException = exception as HttpException;
if (httpException == null)
{
// should redirect to common error page
}
else //It's an Http Exception, Let's handle it.
{
switch (httpException.GetHttpCode())
{
case 404:
// Page not found.
// redirect to another error page if need
break;
case 500:
// Server error.
// redirect to another error page if need
break;
default: // covers all other http errors
// redirect to another error page if need
break;
}
}

关于c# - ASP.NET MVC 5中发生任何错误时,重定向到 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32444726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com