C# ASP.net WebApi-格式错误的post数据的自定义消息
我有一个MVC4WebAPI应用程序。我要做的是过滤掉由于put/post期间发送的数据格式错误而发生的任何C# ASP.net WebApi-格式错误的post数据的自定义消息,c#,.net,asp.net-mvc,asp.net-web-api,C#,.net,Asp.net Mvc,Asp.net Web Api,我有一个MVC4WebAPI应用程序。我要做的是过滤掉由于put/post期间发送的数据格式错误而发生的任何ModelState错误 我有一个ActionFilterAttribute,它检查ModelState是否有效。我想将状态的ErrorMessages发送回用户。这部分工作正常 /// <summary> /// This filter will validate the models that are used in the webapi /// </summary&
ModelState
错误
我有一个ActionFilterAttribute
,它检查ModelState
是否有效。我想将状态的ErrorMessage
s发送回用户。这部分工作正常
/// <summary>
/// This filter will validate the models that are used in the webapi
/// </summary>
public class MyValidationFilter :System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
//ErrorResponse is just a simple data structure used to hold response
ErrorResponse errorResponse = new ErrorResponse();
//loop through each key(field) and see if it has any errors
foreach (var key in actionContext.ModelState.Keys)
{
var state = actionContext.ModelState[key];
if (state.Errors.Any())
{
string validationMessage = state.Errors.First().ErrorMessage;
errorResponse.ErrorMessages.Add(new ErrorMessage(validationMessage));
}
}
//this is a custom exception class that i have that sends the response to the user.
throw new WebAPIException(HttpStatusCode.BadRequest, errorResponse );
}
}
}
但是,如果有人传递格式不正确的XML或JSON数据,我无法控制消息。如果那样的话,我可能会
未终止的字符串。应为分隔符:\”.Path“”,第1行,位置
九,
或
分析属性名称后的字符无效。应为“:”,但得到:
}.路径“”,第1行,位置9
或者这些显示我的名称空间的
将值“Medium”转换为类型时出错
“MyNameSpace.Tenant.webapi.Library.IndividualRegistrationInfo.”路径
'',第1行,第8位
第1行位置7出错。应为元素
来自命名空间的“IndividualRegistrationInfo”
'http://schemas.datacontract.org/2004/07/MyNameSpace.Tenant.WebAPIs.Library.IndividualRegistrationInfo'..
遇到名称为“asdf”的“元素”,命名空间为“”
发生这种情况时,我想以某种方式发回一条通用的“无效数据”消息。是否有其他筛选器可供我使用,或者有其他地方可捕获并覆盖这些消息
更新
以下是我根据Chris的建议所做的事情:
我为JSON和XML创建了两个新的格式化程序
public class JsonFormatter : JsonMediaTypeFormatter
{
public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
System.Threading.Tasks.Task<object> task = base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
//parse error if null
if (task.Result == null)
{
//handle error here.
}
return task;
}
}
public class XMLFormatter : XmlMediaTypeFormatter
{
public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
System.Threading.Tasks.Task<object> task = base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
//parse error if null
if (task.Result == null)
{
//handle error here
}
return task;
}
}
public class JsonFormatter : JsonMediaTypeFormatter{
public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
System.Threading.Tasks.Task<object> task = base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
//parse error if null
if (task.Result == null)
{
//handle error here.
}
return task;
}}
我不确定是否有更好的方法,但这似乎是可行的。我不完全确定,但您可能只需重载默认的
JsonMediaTypeFormatter
和XmlMediaTypeFormatter
实现,并用自己的异常包装它们的异常。这有助于直接绑定您的异常处理查找问题的根源(即格式化程序),而不是尝试通过IEExceptionFilter
来处理问题。以下是我根据Chris的建议所做的工作:
为JSON和XML创建了2个新的格式化程序
public class JsonFormatter : JsonMediaTypeFormatter
{
public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
System.Threading.Tasks.Task<object> task = base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
//parse error if null
if (task.Result == null)
{
//handle error here.
}
return task;
}
}
public class XMLFormatter : XmlMediaTypeFormatter
{
public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
System.Threading.Tasks.Task<object> task = base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
//parse error if null
if (task.Result == null)
{
//handle error here
}
return task;
}
}
public class JsonFormatter : JsonMediaTypeFormatter{
public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
System.Threading.Tasks.Task<object> task = base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
//parse error if null
if (task.Result == null)
{
//handle error here.
}
return task;
}}
在global.asax中的应用程序中
GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonFormatter());
GlobalConfiguration.Configuration.Formatters.Insert(1, new XMLFormatter());
GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonFormatter());
GlobalConfiguration.Configuration.Formatters.Insert(1, new XMLFormatter());
我不确定是否有更好的方法,但这似乎很有效。所以这里有一个难点:
MediaTypeFormatter
,它使用与HTTP请求相关联的System.IO.Stream
ifformatterlogger.OnError(字符串,异常)
,但第一个参数只是一个“路径”(不管解析器如何选择它),而不是输入本身的副本因此,是的,如果您想查看并响应(或记录)导致故障的请求,您需要实现并注册您自己的MediaTypeFormatter,执行您自己的解析/反序列化,并根据您自己的协议(除了WebApi协议)处理它.我无法直接找到发生的错误,但我确实创建了自己的格式化程序。我能够判断何时存在格式错误的数据,但可能有更好的方法。