2017-10-09 2 views
0

Ich arbeite an einer ASP.net-Website, die eine Fehlerantwort im httpcontext zurücksenden muss, wenn eine nicht behandelte Ausnahme auftritt. Ich kann nicht auf eine andere Seite umleiten, da der Verbraucher meine Website nur für Anmeldezwecke anstößt und keine andere Seite als die Antwort sieht.ASP.Net unbehandelt Exception-Handler zu manipulieren Antwort

Ich dachte an die Verwendung System.Web.Http.ExceptionHandler, aber meine Website läuft aus .NET Framework 3.5 und kann nicht verwendet werden.

Kann mir jemand eine Idee geben, was ich tun kann, brauche ich ein gemeinsames HTTP-Modul, das alle Ausnahmen abfängt und dann eine HTTP-Antwort mit einer Fehlermeldung im folgenden Format sendet.

<response> 
    <error> 
    <code>101</code> 
    <description>error from the exception</description> 
    </error> 
</response> 

Antwort

0

ich folgendes in Global.asax Code haben (von VB umgewandelt)

protected void Application_Error(object sender, EventArgs e) 
{ 
    Exception exception = Server.GetLastError(); 
    Server.ClearError(); 

    Logging.LogError(exception); 
    //Get a HTML file stored in resources which contains my error text and some tokens to replace. 
    dynamic html = My.Resources.ErrorText; 
    html = html.Replace("{Title}", Server.HtmlEncode(My.Application.Info.Title)); 
    html = html.Replace("{Product}", Server.HtmlEncode(My.Application.Info.ProductName)); 
    html = html.Replace("{Version}", Server.HtmlEncode(My.Application.Info.Version.ToString)); 

    //Build my exception message looping though all the inner exceptions 
    dynamic errorDetails = exception.Message + Constants.vbNewLine + exception.StackTrace; 

    while (exception.InnerException != null) { 
     exception = exception.InnerException; 
     errorDetails += Constants.vbNewLine + Constants.vbNewLine + "Inner Exception: " + Constants.vbNewLine + exception.Message + Constants.vbNewLine + exception.StackTrace; 
    } 

    //Replace the {ErrorDetails} tag with my error text 
    html = html.Replace("{ErrorDetails}", Server.HtmlEncode(errorDetails).Replace(Constants.vbNewLine, "<br/>")); 

    //Replace the logo tag 
    using (IO.MemoryStream ms = new IO.MemoryStream()) { 
     using (img == My.Resources.Logo) { 
      img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
      dynamic logoBytes = ms.ToArray; 
      html = html.Replace("{Logo}", "data:image/png;base64," + Convert.ToBase64String(logoBytes, 0, logoBytes.Length)); 
     } 
    } 

    //Replace the response 
    Response.Clear(); 
    Response.Write(html); 
    Response.Flush(); 
} 

Es sollte möglich sein, einen Event-Handler zum HttpApplication.Error Ereignis in der init eines Httpmodule hinzuzufügen das Gleiche tun. Siehe https://msdn.microsoft.com/en-us/library/system.web.httpapplication_events(v=vs.110).aspx, die an die Init-Methode übergeben wird https://msdn.microsoft.com/en-us/library/system.web.ihttpmodule(v=vs.110).aspx

+0

Ist es möglich, es in einem HTPmodul zu tun, damit ich es wiederverwendbar halten kann? – Ramki

+0

Sieht so aus, als wäre es möglich, dem Ereignis HttpApplication.Error in der Init eines HTTP-Moduls einen Ereignishandler hinzuzufügen. Siehe https://msdn.microsoft.com/en-us/library/system.web.httpapplication_events(v=vs.110).aspx und https://msdn.microsoft.com/en-us/library/system. web.ihttpmodul (v = vs.110) .aspx – apc