Hai bisogno di aiuto con un ASP.NET MVC IExceptionFilter personalizzato
-
07-07-2019 - |
Domanda
Sto cercando di creare il mio ExceptionFilter. Immediatamente, ASP.NET MVC viene fornito con l'attributo [HandleError]. Questo è fantastico - > ma restituisce un errore html Visualizza.
In quanto tale, voglio restituire un messaggio di errore json. Quindi sto facendo il mio.
Ora, tutto funziona alla grande fino a quando non collaudo il mio URL. Continuo a ricevere un errore. questo è il messaggio ....
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 - non ha senso. Lasciamo un po 'di codice per spiegare cosa sto cercando di fare, quindi ...
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;
}
}
Quindi questo è il mio codice .... ed è un po 'funzionante ma non lo è.
Cosa significa questa cosa 'chiave'? cosa mi sono perso, cercando di fare?
Per favore aiutate!
Soluzione
Trovato la mia risposta - > ho dovuto inserire l'URL in " .. " (virgolette) altrimenti cerca di eseguire qualsiasi cosa sia dopo il simbolo della e commerciale, come comando o qualcosa del genere.
non so perché, ma questo lo risolve.