public static async Task SendErrorResponseAsync(HttpResponse response, string title, IHostingEnvironment env, Action <XmlWriter> writeMessage = null, Exception exception = null, int statusCode = StatusCodes.Status500InternalServerError) { using (ResponseDocument responseDocument = new ResponseDocument(title)) { string cssClass = (exception != null) ? "danger" : "warning"; responseDocument.BodyWriter.WriteStartElement("h1"); responseDocument.BodyWriter.WriteAttributeString("class", cssClass); responseDocument.BodyWriter.WriteString(responseDocument.Title); responseDocument.BodyWriter.WriteEndElement(); if (writeMessage != null) { responseDocument.BodyWriter.WriteStartElement("div"); responseDocument.BodyWriter.WriteAttributeString("class", "alert-" + cssClass); if (writeMessage != null) { writeMessage(responseDocument.BodyWriter); } responseDocument.BodyWriter.WriteEndElement(); } if (exception != null && env.IsDevelopment()) { responseDocument.BodyWriter.WriteStartElement("dl"); responseDocument.BodyWriter.WriteElementString("dt", "Exception Type"); responseDocument.BodyWriter.WriteElementString("dd", exception.GetType().FullName); responseDocument.BodyWriter.WriteElementString("dt", "Message"); responseDocument.BodyWriter.WriteElementString("dd", exception.Message); responseDocument.BodyWriter.WriteEndElement(); try { if (!String.IsNullOrWhiteSpace(exception.StackTrace)) { responseDocument.BodyWriter.WriteElementString("h3", "Stack trace"); responseDocument.BodyWriter.WriteElementString("pre", "exception.StackTrace"); } } catch { } } responseDocument.StatusCode = statusCode; await SendHtmlResponseAsync(response, responseDocument); } }
public static async Task SendHtmlResponseAsync(HttpResponse response, ResponseDocument document) { await SendTextResponseAsync(response, document.GetHtml(), new ContentType(MediaTypeNames.Text.Html), document.StatusCode); }