ASP.Net的Web API:格式化参数绑定异常 [英] ASP.Net Web API: Formatter Parameter Binding exception

查看:943
本文介绍了ASP.Net的Web API:格式化参数绑定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Web API操作方法接受作为动作参数DataContract。

I have a DataContract which my Web API action method accepts as an action parameter.

public HttpResponseMessage PostMyObject(MyObjectRequestDc objRequest){ ... }

[DataContract]
public class MyObjectRequestDc
{
    public MyObjectRequestDc()
    {
        References = new List<Uri>();
    }
    [DataMember]
    public List<Uri> References { get; set; }
}

一个合同的属性是('参考')的URI的对象的列表。

One of the properties of the contract is a list of URI objects ('References').

如果客户曾经提交其中包含无法解析为一个正确的URI字符串的请求时,一个异常深刻的框架code中抛出(因为它无法从给定的字符串实例URI类)

If the client ever submits a request which contains a string that does not resolve to a correct URI, an exception is thrown deep within the framework code (because it can't instantiate the URI class from the given string):

示例的Json输入:

{参考:这不是一个有效的URI。 ]}

{ "References": [ "This is not a valid uri." ] }

异常详细信息/堆栈跟踪

Error details: System.InvalidOperationException: This operation is not supported for a relative URI.
at System.Uri.get_AbsolutePath()
at GetAbsolutePathFromUri(Object )
at System.Web.Http.Metadata.Providers.AssociatedMetadataProvider`1.<>c__DisplayClass3.<GetMetadataForPropertiesImpl>b__0()
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateProperties(ModelMetadata metadata, ValidationContext validationContext)
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateElements(IEnumerable model, ValidationContext validationContext)
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateProperties(ModelMetadata metadata, ValidationContext validationContext)
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)
at System.Web.Http.ModelBinding.FormatterParameterBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(Object model)
at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass49.<ToAsyncVoidTask>b__48()
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)

要求是用户返回与code 400(错误请求),并沿行消息的回应被引用的URI'XXX'是无效的。

The requirement is that the user be returned a Response with code 400 (BadRequest) and a message along the lines of "The referenced URI 'xxx' is not valid".

目前,该异常被抓住GlobalExceptionFilter并返回一个无用的500。

At the moment, the exception is caught by the GlobalExceptionFilter and a unhelpful 500 is returned.

在理想情况下,我想这种情况下被捕获为模型状态错误或其他一些机制可以让我控制的响应。

Ideally, I'd like this scenario to be captured as a Model State error or some other mechanism which will allow me to control the response.

选项,我认为:
1.创建一个自定义HttpParameterBinding类和处理异常呢?
2.将引用属性为String对象的列表,然后控制器的动作方法中处理URI类的实例化
3.抓住在GlobalExceptionFilter异常显式地和裁缝从这里的响应(这有一个等级code气味,虽然...)

Options I've considered: 1. Create a custom HttpParameterBinding class and handle the exception in there? 2. Change the References property to be a list of String objects and then handle the instantiation of the URI class within the action method of the Controller 3. Catch the exception in the GlobalExceptionFilter explictly and tailor the response from here (this has a rank code smell though...)

建议最受大家欢迎!

推荐答案

鉴于目前似乎还没有答案的问题,工作都是围绕我要聘请我第二个选项:

Given that there appears to be no answer to the problem, the work around I am going to employ is my second option:

更改参考属性为String对象的列表,然后控制器

Change the References property to be a list of String objects and then handle the instantiation of the URI class within the action method of the Controller

和再处理URI自己的实例化过程中出现的任何异常...

And then handle any exceptions that occur during the instantiation of the URI myself...

这篇关于ASP.Net的Web API:格式化参数绑定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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