我可以访问.net Web API模型绑定无法处理的数据吗? [英] Can I get access to the data that the .net web api model binding was not able to handle?

查看:98
本文介绍了我可以访问.net Web API模型绑定无法处理的数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 4 Web api应用程序,该应用程序接收json对象,并使用标准模型绑定工具将它们绑定到我的poco对象,以便在控制器方法中轻松使用.

I have a MVC 4 web api application that receives json objects and uses the standard model binding facility to bind them to my poco objects for easy use in my controller methods.

当我接收的json结构直接映射到我的poco时,这一切都很好.例如,我有一个控制器方法

This all works great, when the json structures I am receiving map directly to my poco. for example I have a controller method

 public HttpResponseMessage Post(DeviceConfigurationDto deviceConfiguration)

并且DeviceConfigurationDto是

and DeviceConfigurationDto is

public class DeviceConfigurationDto
    {
        public long Timestamp { get; set; }
        public string DeviceType { get; set; }
        public string AssetName { get; set; }
    }
}

然后我发布以下JSON

and I post the following JSON

 {
        "Timestamp": 234234234,
        "DeviceType": "A555tom",
        "AssetName": "HV103"
    }

内置的模型绑定工具在处理变体方面做得很好,例如多余的字段或缺少的字段等等.但是,如果您将其推到很远的地方.例如通过发布以下json

The built in model binding facility does a pretty good job of working with variations, like extra fields or fields missing and so forth. But if you push it to far. for example by posting the following json

    [
    {
        "Fields4": "asda",
        "Timestamp": 234234234,
        "DeviceType": "A555tom",
        "AssetName": "HV103"
    },
    {
        "Timestamp": 234234234,
        "DeviceType": "A555tom",
        "DedviceType": "A555tom",
        "AssetName": "HV103"
    }
]

它最终崩溃了,我最终将方法中的参数设置为null.

It eventually falls over and I end up with the parameter into my method being null.

是否有一种方法可以在数据按预期方式工作时进行模型绑定,但是还为我提供了获取模型绑定程序有问题的数据的能力,因此我可以记录以下请求:我收到与预期不符的东西吗?

Is there a way for the model binding to work when the data is as expected, but then also provide me with the ability to get to the data that was a problem for the model binder, so that I can log the request that I am receiving that are not matching what I expected?

谢谢

推荐答案

一种方法是使用自定义ActionFilter.由于模型绑定是在执行操作过滤器之前发生的,因此您可以在自定义操作过滤器中获取操作参数.

One way to do this is to use custom ActionFilter. Since model binding happens before an action filter is executed, you can get your action argument(s) in your a custom action filter.

例如:

public class LogFailToModelBindArgActionFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext context)
    {
        var deviceConfigurationArg = context.ActionArguments["deviceConfiguration"];
        if (deviceConfigurationArg == null) // fail to model bind data to argument ...
        {
            var jsonContent = context.Request.Content.ReadAsStringAsync().Result; // calling .Result here for sake of simplicity...
            Log(jsonContent);
        }
    }
}

操作:

    [LogFailToModelBindArgActionFilter]
    public HttpResponseMessage Post(DeviceConfigurationDto deviceConfiguration)
    {...}

这篇关于我可以访问.net Web API模型绑定无法处理的数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆