2016-11-30 2 views
2

Der folgende Ausnahmefilter leitet Ausnahmen zu meiner spezifischen Fehlerseite um.Ausnahmebestimmungen, die einen leeren Body in asp.net-core zurückgeben 1.1

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)] 
public class FrontofficeControllerExceptionFilterAttribute : ExceptionFilterAttribute 
{ 
    protected readonly ILogger<FrontofficeController> _logger; 
    protected readonly IHostingEnvironment _hostingEnvironment; 
    protected readonly IModelMetadataProvider _modelMetadataProvider; 

    public FrontofficeControllerExceptionFilterAttribute(ILogger<FrontofficeController> logger, IHostingEnvironment hostingEnvironment, IModelMetadataProvider modelMetadataProvider) 
    { 
     _logger = logger; 
     _hostingEnvironment = hostingEnvironment; 
     _modelMetadataProvider = modelMetadataProvider; 
    } 

    public override void OnException(ExceptionContext context) 
    { 
     if (context.ExceptionHandled) return; 

     Exception ex = context.Exception; 
     var result = new ViewResult { ViewName = "ApplicationError" }; 

     context.ExceptionHandled = true; // mark exception as handled 
     context.Result = result; 
    } 
} 

Nach meinem Web-App zu asp.net Kern 1.1 das Ergebnis der Migration ist eine leere Seite: Der Körper der Antwort ist leer und Content-Length ist gleich Null. Dies ist die Antwort auf dem Client empfangen

HTTP/1.1 200 OK 
Server: Kestrel 
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcaW50ZXJhaC52aXN1YWxzdHVkaW8uY29tXEludmVudGFyaW9cTWFpblxTb3VyY2VcRnJvbnRvZmZpY2Vcc3JjXEZyb250b2ZmaWNl?= 
X-Powered-By: ASP.NET 
Date: Wed, 30 Nov 2016 14:49:53 GMT 
Content-Length: 0 

habe jemand ein ähnliches Problem erlebt und warum? danke für irgendeinen Kommentar

+0

Ich lasse hier ein Update, wenn jemand das gleiche Problem hat: 1), beginnend mit einer frischen und leerer neuen App mit aspnetcore 1.0.1 der Seite korrekt 2) kommentiert die Zeile ‚wiedergegeben wird context.ExceptionHandled = wahr;' Die Seite wird gerendert so scheint es wirklich, dass das Problem mit asp.net core.1.1 und 'context.ExceptionHandled' ist –

Antwort

1

das Verhalten wird von Github Aspnet/MVC-Team bestätigt. https://github.com/aspnet/Mvc/issues/5594#issuecomment-265866653

Im Moment ist die einzige Lösung, eine HTML-Antwort direkt zu rendern. In diesem Fall funktioniert die Markierung ExceptionHandled = true wie erwartet.

public override void OnException(ExceptionContext context) 
    { 
     context.ExceptionHandled = true; // mark exception as handled 
     context.HttpContext.Response.Clear(); 
     context.HttpContext.Response.StatusCode = 400; 
     context.HttpContext.Response.ContentType = new MediaTypeHeaderValue("text/html").ToString(); 
     var home = string.Format("{0}://{1}", context.HttpContext.Request.Scheme,context.HttpContext.Request.Host); 
     var htmlString ="<h2>SI E' VERIFICATO UN ERRORE INATTESO</h><br/><br/>"; 
     context.HttpContext.Response.WriteAsync(htmlString, Encoding.UTF8); 
    } 
Verwandte Themen