Brauchen Sie Hilfe mit einem benutzerdefinierten ASP.NET MVC IExceptionFilter
-
07-07-2019 - |
Frage
Ich versuche, meinen eigenen ExceptionFilter zu machen. Out of the box, kommt ASP.NET MVC mit dem [Handle] Attribut. Das ist großartig. -> aber es gibt einige HTML-Fehler anzeigen
Als solche, ich will etwas json Fehlermeldung zurück. Also bin ich mein eigenes zu machen.
Nun, alles funktioniert gut, bis ich meine url testen. Ich erhalte eine Fehlermeldung. das ist die Botschaft ....
C:\Temp\curl-7.19.5>curl -i http://localhost:6969/search/foo?name=1234&key=test1xxx
HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 14 Sep 2009 01:54:52 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 6
Connection: Close
"Hi StackOverflow"'key' is not recognized as an internal or external command,
operable program or batch file.
C:\Temp\curl-7.19.5>
Ok - das macht keinen Sinn. Lässt irgend Code lassen zu erklären, was ich versuche zu tun, dann ...
public class HandleErrorAsJson : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// Snip normal checks and stuff...
// Assume we've figured out the type of error this is.
// I'm going to hardcode it here, right now.
int statusCode = 401;
string message = "Hi StackOverflow";
// Now prepare our json output.
filterContext.Result = new JsonResult
{
Data = message
};
// Prepare the response code.
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = statusCode;
}
}
Das ist also mein Code .... und es ist sorta funktioniert, aber es ist nicht.
Was bedeutet dieser ‚Schlüssel‘ bedeuten? Was habe ich verpasst haben, versuchen zu tun?
Bitte helfen Sie!
Lösung
Gefunden meine Antwort -.> Ich hatte die URL in setzen „..“ (Anführungszeichen) sonst versucht, es zu laufen, was nach dem Ampersand-Symbol ist, als Befehl oder etwas
nicht sicher, warum, aber das behebt es.