如何允许 MVC ActionFilter 查看“无效的 JSON 原语”原始数据是什么
本文关键字:JSON 无效的 原语 是什么 原始数据 无效 MVC 何允许 ActionFilter 查看 | 更新日期: 2023-09-27 18:31:29
我有以下自定义 [JsonFilter] 的操作方法,当客户端发送的 CustomClass 的 JSON 结构无效时,不会调用此 ActionFilterAttribute:
[JsonFilter(Param = "myCustomClass", JsonDataType = typeof(CustomClass)]
[ValidateInput(false)]
public ActionResult MyFunction(CustomClass myCustomClass)
{
//Do something ground-breaking here
}
[Json] ActionFilterAttribute 代码只有在客户端传递的 JSON 结构有效时才会被调用。如果客户端发送的 JSON 结构无效,则 MVC 框架管道似乎会引发以下异常,并且我的 ActionFilter 逻辑永远不会被调用(希望为错误的类类型发回一个特殊的错误代码)。这是我得到的异常,不希望这些类/事件在调用我的操作方法时尝试查看 JSON 数据:
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
我还尝试将以下内容添加到类/属性中:
[ScriptIgnore]
但这也没有用。如果我从操作方法中删除参数,那么我的方法将有机会查看传入的数据,但是当我有一个专用的 JSON 操作筛选器将 JSON 转换为 C# 类时,我需要访问 JSON 的请求对象。 关于如何防止框架在我的操作过滤器开始之前尝试解析此 JSON 的任何建议?
哈姆雷特,你的答案是对的。谢谢一百万。我使用这篇文章来了解有关实现此类过滤器的更多详细信息,并且可以在操作方法级别应用:自定义异常过滤器未在 MVC 中命中 asp.net