5

我有一个 MVC 4 WebAPI 应用程序。我想要做的是过滤掉ModelState由于放置/发布期间发送的格式错误的数据而发生的任何错误。

我有一个ActionFilterAttribute检查是否ModelState有效。我想将状态的ErrorMessages 发回给用户。这部分工作正常。

/// <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 );

        }

    }
}

正常验证(必需、字符串长度、正则表达式)都可以正常工作,因为我可以控制这些消息。

[Required(ErrorMessage = "ID is required")]
public string ID { get; set; }

但是,如果有人传递了格式不正确的 XML 或 JSON 数据,我将无法控制该消息。如果发生这种情况,那么我可能会得到

未终止的字符串。预期分隔符:\"。路径 '',第 1 行,第 9 位。

或者

解析属性名称后出现无效字符。应为“:”,但得到:}。路径'',第 1 行,位置 9。

或者这些显示我的命名空间的那些。

将值“Medium”转换为“MyNameSpace.Tenant.WebAPIs.Library.IndividualRegistrationInfo”时出错。路径 '',第 1 行,位置 8。

第 1 行位置 7 出错。从命名空间“http://schemas.datacontract.org/2004/07/MyNameSpace.Tenant.WebAPIs.Library.IndividualRegistrationInfo”中期待元素“IndividualRegistrationInfo”。遇到名称为“asdf”的“元素” , 命名空间 ''

发生这种情况时,我想以某种方式发回通用的“无效数据”消息。是否有另一个我可以使用的过滤器,或者其他可以捕获和覆盖这些消息的地方?

更新

这是我根据克里斯的建议最终做的事情:

我为 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;
    }
}

Application_Start在 global.asax 中的方法中

GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonFormatter());
GlobalConfiguration.Configuration.Formatters.Insert(1, new XMLFormatter());

我不确定是否有更好的方法,但这似乎有效。

4

3 回答 3

0

我不完全确定,但您可能只是重载默认值JsonMediaTypeFormatterXmlMediaTypeFormatter实现,并用您自己的异常包装它们的异常。这样做的好处是将您的异常处理直接与问题的根源(即格式化程序)联系起来,而不是尝试通过IExceptionFilter.

于 2012-08-17T11:39:35.477 回答
0

这是我根据克里斯的建议最终做的事情:

为 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 ReadFromStreamAsync(Type type, System.IO.Stream stream, System.Net.Http.Headers.HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
    {

        System.Threading.Tasks.Task task = base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);

        //parse error if null
        if (task.Result == null)
        {
            //handle error here
        }

        return task;
    }
}

并在 global.asax 的 Application_Start 方法中


GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonFormatter());
GlobalConfiguration.Configuration.Formatters.Insert(1, new XMLFormatter());

我不确定是否有更好的方法,但这似乎有效。

于 2012-08-23T14:44:28.737 回答
0

所以这里的问题是:

  1. 它是MediaTypeFormatter消耗System.IO.Stream与 HTTP 请求相关联的;
  2. 虽然该格式化程序将调用IFormatterLogger.OnError(string,Exception),但第一个参数只是一个“路径”(无论解析器选择如何构想它),而不是输入本身的副本。

所以,是的,如果您想查看和响应(或记录)导致错误的请求,您需要实现并注册自己的 MediaTypeFormatter,进行自己的解析/反序列化,并根据自己的情况进行处理协议(除了 WebApi 协议)。

于 2014-09-23T09:53:29.530 回答