Gibt es eine vollständige Lösung, um Fehler wie 404, 500 usw. auf IIS Express und IIS 7.5 wirklich zu behandeln?C# MVC Fehlerbehandlung von IIS Express und IIS 7.5
ich nicht verzählt haben, wie viele Artikel, die ich gelesen habe, in Bezug auf Web.config, custom Ein, Aus, etc. drehen ... oder Kommentierung/uncommenting filters.Add(new HandleErrorAttribute());
vom FilterConfig.cs
Kann jemand bitte überprüfen, was ich Bis jetzt und sagen Sie mir, was die richtige Konfiguration ist, um Serverfehler sowohl auf IIS Express als auch auf IIS 7.5 vollständig zu erfassen, zeigen Sie eine benutzerdefinierte Fehlerseite und nicht die Shared/Error.cshtml an, die anscheinend egal was und Application_Error aufgerufen wird wird ignoriert.
Global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
var lastError = Server.GetLastError();
Server.ClearError();
var statusCode = lastError.GetType() == typeof(HttpException) ? ((HttpException)lastError).GetHttpCode() : 500;
var httpContextWrapper = new HttpContextWrapper(Context);
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("statusCode", statusCode);
routeData.Values.Add("exception", lastError);
IController errorController = new ErrorController();
var requestContext = new RequestContext(httpContextWrapper, routeData);
errorController.Execute(requestContext);
Response.End();
}
ErrorController.cs
public class ErrorController : Controller
{
private readonly Common _cf = new Common();
private readonly string _httpReferer = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
private readonly string _httpUserAgent = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
public ActionResult Index(int? statusCode, Exception exception)
{
Response.TrySkipIisCustomErrors = true;
try
{
Response.StatusCode = ((HttpException) exception).GetHttpCode();
}
catch (Exception tryException)
{
SendEmail(tryException, statusCode.ToString());
return Redirect("/");
}
SendEmail(exception, Convert.ToString(Response.StatusCode));
ViewBag.Title = statusCode == 404 ? "Page Not Found" : "Server Error";
ViewBag.ExceptionMessage = Convert.ToString(exception.Message);
return View("Index");
}
private void SendEmail(Exception exception, string errorType)
{
const string to = "[email protected]";
const string @from = "[email protected]";
var subject = "SendEmailError (" + errorType + ")";
var stringBuilder = new StringBuilder();
stringBuilder.Append("<p><strong>Exception Message: </strong>" + exception.Message + "</p>");
stringBuilder.Append("<p><strong>Source: </strong>" + exception.Source + "</p>");
stringBuilder.Append("<p><strong>Referer: </strong>" + _httpReferer + "</p>");
stringBuilder.Append("<p><strong>IP Address: </strong>" + _cf.GetIpAddress() + "</p>");
stringBuilder.Append("<p><strong>Browser: </strong>" + _httpUserAgent + "</p>");
stringBuilder.Append("<p><strong>Target: </strong>" + exception.TargetSite + "</p>");
stringBuilder.Append("<p><strong>Stack Trace: </strong>" + exception.StackTrace + "</p>");
stringBuilder.Append("<p><strong>Inner Exception: </strong>" + (exception.InnerException != null ? exception.InnerException.Message : "") + "</p>");
var body = stringBuilder.ToString();
_cf.SendEmail(subject, to, from, body, null, true, null, null);
}
}
Index.cshtml
@model WebApplication1.Models.Error
@if (HttpContext.Current.IsDebuggingEnabled)
{
if (Model != null)
{
<p><strong>Exception:</strong> @Model.Exception.Message</p>
<div style="overflow:scroll">
<pre>@Model.Exception.StackTrace</pre>
</div>
}
else
{
<p><strong>Exception:</strong> @ViewBag.ExceptionMessage</p>
}
}
Error.cs
using System;
namespace WebApplication1.Models
{
public class Error
{
public int HttpStatusCode { get; set; }
public Exception Exception { get; set; }
}
}
Jede Hilfe viel :-)
Ich aktualisiere dies nur, aber bitte versuchen Sie, diese Zeile zuerst kommentieren ... okay, mit nur diesen einfachen Änderungen, funktioniert es für Sie? – VictorySaber
Danke, ich habe das HandleErrorAttribute auskommentiert und meinem webconfig ' customErrors> 'aber immer noch keine Freude, den ErrorController überhaupt nicht zu treffen. –
iggyweb
Versuchen Sie, das PassThrough zum webconfig hinzuzufügen – VictorySaber