Frage

In meinem MVC Web-Projekt. Ich versuche, benutzerdefinierte Fehlerseiten zu meinen Besuchern zu zeigen, ohne „custromerrors“ Element in web.config verwendet wird.

Ich kann Ausnahmen wie unter

fangen
protected void Application_Error(object sender, EventArgs e)
{

    Exception exception = Server.GetLastError();

    bool success = RaiseErrorSignal(exception);

    Response.Clear();

    HttpException httpException = exception as HttpException;

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Error");

    if (httpException == null)
    {
        routeData.Values.Add("action", "Index");
    }
    else //It's an Http Exception, Let's handle it.
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                // Page not found.
                routeData.Values.Add("action", "Error404");
                break;
            case 500:
                // Server error.
                routeData.Values.Add("action", "Error500");
                break;

            // Here you can handle Views to other error codes.
            // I choose a General error template  
            default:
                routeData.Values.Add("action", "Index");
                break;
        }
    }

    // Pass exception details to the target error View.
    routeData.Values.Add("error", exception);

    // Clear the error on server.
    Server.ClearError();

    // Call target Controller and pass the routeData.
    IController errorController = new ProjectName.WebSite.Controllers.ErrorController();
    errorController.Execute(new RequestContext(
         new HttpContextWrapper(Context), routeData));


}

private static bool RaiseErrorSignal(Exception e)
{
    var context = HttpContext.Current;
    if (context == null)
        return false;
    var signal = ErrorSignal.FromContext(context);
    if (signal == null)
        return false;
    signal.Raise(e, context);
    return true;
}

Aber Elmah kippt Fehler protokolliert auch i Fehlersignal am erhöhen.

War es hilfreich?

Lösung

fand ich das Problem, ich habe einen web.config Abschnitt verpasst. Ich fügte hinzu, "ErrorLog" Modul <system.webserver><modules>.

Ich brauche es auch zu <system.web><httpModules> hinzuzufügen.

Nach dem Hinzufügen Elmah startet Fehler zu protokollieren.

Auch brauche ich nicht ErrorSignal.Raise () -Methode aufrufen, können Elmah Fehler erkennen ohne Signalisierung.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top