2010-08-02 4 views

Répondre

2

Utilisez les pages standard d'erreur ASP.NET (activer dans web.config):

<customErrors mode="On|RemoteOnly" defaultRedirect="/error/problem"> 
    <error statusCode="404" redirect="/error/notfound"/> 
    <error statusCode="500" redirect="/error/problem"/> 
</customErrors> 

et/ou créer un contrôleur de gestion des erreurs et de l'utiliser avec une route catchall:

ErrorController.cs :

public class ErrorController : Controller 
{ 
    public ActionResult NotFound(string aspxerrorpath) 
    { 
     // probably you'd like to log the missing url. "aspxerrorpath" is automatically added to the query string when using standard ASP.NET custom errors 
     // _logger.Log(aspxerrorpath); 
     return View(); 
    } 
} 

global.asax:

// This route catches all urls that do not match any of the previous routes. 
// So if you registered the standard routes, somthing like "/foo/bar/baz" will 
// match the "{controller}/{action}/{id}" route, even if no FooController exists 
routes.MapRoute(
    "Catchall", 
    "{*catchall}", 
    new { controller = "Error", action = "NotFound" } 
); 
+0

J'ai ajouté le fichier routes.MapRoute() et cela ne fonctionne pas. Des idées pourquoi? J'ai déjà fait le contrôleur et la vue. –

+0

La route Catchall n'attrape que les routes qui ne correspondent à aucune autre route. Donc, si vous avez la route standard "{controller}/{action}/{id}", chaque URL ressemblant à ceci "/ foo/bar/baz" correspondra à cette route, même si aucun FooController n'existe. Pour détecter tous les contrôleurs/actions manquants, la solution la plus simple consiste à utiliser les erreurs personnalisées ASP.NET. Vous utilisez des fichiers statiques pour la page d'erreur ou créez un contrôleur/action pour cela (en utilisant mon exemple ci-dessus vous redirigiez vers le ErrorController et appelez l'action NotFound). J'ai édité ma réponse pour inclure cela. – davehauser

+0

Pour plus de détails sur ce sujet jeter un oeil à cette question: http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/2577095#2577095 – davehauser

Questions connexes