ich alle am Umgang mit den Fehlern in Controllern mit dem Handle Attribut, die ich für geändert haben für http und Ajax Anfragen, und als 500 Fehlertyp Handhabung.Handle Fehler für Http und Anfragen Ajax Handle Klasse Attribut
public class HandleExceptionsAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
return;
else
{
ErrorLogger.LogException(filterContext);
if (filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Exception != null)
{
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
filterContext.Result = new JsonResult
{
Data = new
{
success = false,
message = "error",
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
base.OnException(filterContext);
}
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
}
}
}
und es auf jedem Controller wie mit:
[HandleExceptions]
public class InvoiceController : Controller
{}
Ich habe auch Modus auf den Custom in web.config als:
<customErrors mode="On" >
<error statusCode="400" redirect="Home/Error400" />
<error statusCode="401" redirect="Home/Error401" />
<error statusCode="403" redirect="Home/Error403" />
<error statusCode="500" redirect="/Home/Error" />
</customErrors>
und die httperrors wie:
<httpErrors errorMode="Custom" existingResponse="Replace">
<!--<remove statusCode="404" subStatusCode="-1" />-->
<error statusCode="404" path="/Home/Error404" responseMode="ExecuteURL"/>
<!--<remove statusCode="500" subStatusCode="-1" />-->
<error statusCode="500" path="/Home/Error" responseMode="ExecuteURL"/>
</httpErrors>
Das Problem mit mir ist th Wenn ich das Remove-Tag für den Statuscode nur auskommentiere, funktioniert es gut mit httpErrors, aber nicht mit der Ajax-Anfrage, weil es anstelle des Statuscodes die Home/Error-Seite zurückgibt. Aber wenn ich diesen Tag kommentieren dann httpErrors nicht Fehler Seite in den meisten Fällen aber gut für AJAX Anfrage, die ich in statusCode erhalten kann.
$.ajax({
url: "@Url.Action("SomeAction", "SomeController")",
type: "POST",
async: true,
cache: false,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.success) {
catchSuccess("Success", response.responseText);
}
else {
catchWarning("Error", response.responseText);
}
},
Error: function (ex) {
catchError("Unkown Error", "Contact the Administrator");
},
statusCode: {
500: function() {
catchError("Error 500", "Internal Server Error. Contact the Administrator");
},
403: function() {
catchError("Error 403", "Internal Server Error. Contact the Administrator");
},
404: function (response) {
catchError("Error 404", "Internal Server Error. Contact the Administrator");
}
}
});
Was soll ich tun, um beide Seitenfehler zu erfassen? Ajax Anfrage Fehler: Ich möchte den Statuscode Http Anfrage Fehler zurück: Ich möchte die Seite von einer Aktion zurückgeben. (Startseite/Fehler).
Bitte gehen Sie durch http://stackoverflow.com/questions/2480006/what-is-the-difference-between-customerrors-and-httperrors – Amit
Ich bin nicht nur Umgang mit http-Fehlern, aber ich brauche auch Statuscode in Ajax-Anfrage, um Fehler richtig zu behandeln. –
Aber gemäß Ihrem Code, wenn Ajax-Anfrage dort ist, dann geben Sie immer Status als 500 zurück. Sind Sie nicht? – Amit