2015-09-07 2 views
0

Мне нужно перенаправить на страницу при возникновении любой ошибки.Перенаправление на действие контроллера при возникновении какой-либо ошибки в ASP.NET MVC 5

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

ответ

0

Простой способ добавить следующее 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; 
    } 
} 
Смежные вопросы