2016-01-23 3 views
0

J'ai la méthode d'action suivant la coutume [JsonFilter] et ce ActionFilterAttribute n'est pas appelée lorsque la structure JSON pour CustomClass envoyé par le client est invalide:Comment autoriser MVC ActionFilter à voir ce que sont les données brutes "primitive JSON invalide"?

[JsonFilter(Param = "myCustomClass", JsonDataType = typeof(CustomClass)] 
    [ValidateInput(false)] 
    public ActionResult MyFunction(CustomClass myCustomClass) 
    { 
    //Do something ground-breaking here 
    } 

Le [Json] Code ActionFilterAttribute sera seulement est appelé si la structure JSON passée par le client est valide. Si la structure JSON envoyée par le client n'est pas valide, il semble que le pipeline d'infrastructure MVC lève l'exception suivante et que ma logique ActionFilter ne soit jamais appelée (renvoie un code d'erreur spécial pour le type de classe erroné). Voici l'exception que je reçois et je voudrais ne pas avoir ces classes/événements tentent de regarder les données JSON lorsque ma méthode d'action est appelée:

Invalid JSON primitive: <reports the first character that resulting in invalid JSON format here> 
enter code here 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Invalid JSON primitive: . 

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[ArgumentException: Invalid JSON primitive: .] 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +762458 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +360 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) +542 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +222 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +379 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +325 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) +542 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +222 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +115 
    System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +57 
    System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +239 
    System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +211 
    System.Web.Mvc.ControllerBase.get_ValueProvider() +66 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +78 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +153 
    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +1449 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +150 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +96 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +487 
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +45 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +111 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +150 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext) +203 
    System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +879 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +150 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext) +154 
    System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +527 
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +108 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +111 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +150 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext) +203 
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +665 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12289467 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288 

________________________________________ 
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34280 

J'ai aussi essayé d'ajouter ce qui suit à la classe/propriétés:

[ScriptIgnore] 

Mais cela aussi n'a pas fonctionné. Si je supprime le paramètre de ma méthode d'action, ma méthode aura une chance de regarder les données transmises mais je dois alors accéder à l'objet de requête pour le JSON quand j'ai un filtre JSON Action dédié pour convertir le JSON en C# classe. Des suggestions sur la façon d'empêcher le Framework d'essayer d'analyser ce fichier JSON avant que mon ActionFilter ne le fasse?

+1

Essayez d'implémenter un filtre d'exception personnalisé. –

+0

Regardera dans cela. Mais lors de l'inspection initiale, il apparaît qu'il s'agit d'un filtre global, espérant que cela peut être appliqué au niveau de la méthode avec des attributs notant le type de code d'erreur à lancer. Merci encore, publiera mes mises à jour une fois que j'ai quelques détails. –

+0

Hamlet, vous aviez raison à propos de votre réponse. Merci un million. J'ai utilisé ce post pour plus de détails sur la mise en œuvre d'un tel filtre et il peut être appliqué au niveau de la méthode d'action: http://stackoverflow.com/questions/2073925/custom-exception-filter-not-being-hit-in-asp -net-mvc –

Répondre

0

Hamlet, vous avez eu raison de votre réponse. Merci un million. J'ai utilisé ce post pour plus de détails sur l'implémentation d'un tel filtre et il peut être appliqué au niveau de la méthode d'action: Custom Exception Filter not being hit in asp.net MVC