[英]Custom Array Model Binder
I need to understand what I am doing wrong here. 我需要了解我在这里做错了什么。
I have a .net 4.7.2 project. 我有一个.net 4.7.2项目。 I have a Controller that inherits from the System.Web.Http.ApiController class. 我有一个从System.Web.Http.ApiController类继承的Controller。
In the controller I have the following endpoint 在控制器中,我具有以下端点
public HttpResponseMessage EchoIdentifierArray([ModelBinder(typeof(CommaDelimitedArrayBinder))] IEnumerable<int> ids)
{
return Request.CreateResponse(HttpStatusCode.OK, ids);
}
My idea here is simply a test for the id being passed in. 我的想法只是对传递的id进行测试。
The custom model binder is as follows: 定制模型活页夹如下:
public class CommaDelimitedArrayBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
throw new System.NotImplementedException();
}
}
I am using postman to test the end point and I can see the url is in the following format: 我正在使用邮递员来测试终点,并且可以看到该网址的格式如下:
/api/v3/Echos/EchoIdentifierArray?ids=160742,160892 / API / V3 /回声/ EchoIdentifierArray?IDS = 160742,160892
Then place a break point on the first line in the endpoint but I am never getting the exception that i am expecting from the ModelBinder. 然后在端点的第一行上放置一个断点,但我从未遇到过ModelBinder期望的异常。
What am I doing wrong here? 我在这里做错了什么?
I tested your code according to correct name spaces. 我根据正确的名称空间测试了您的代码。 I got issues with object type, reason is matching return type should be bool. 我遇到对象类型问题,原因是匹配的返回类型应该是布尔值。 Please test with below code snippet. 请使用以下代码段进行测试。
public class CommaDelimitedArrayBinder : System.Web.Http.ModelBinding.IModelBinder
{
public CommaDelimitedArrayBinder()
{
}
public bool BindModel(
System.Web.Http.Controllers.HttpActionContext actionContext,
System.Web.Http.ModelBinding.ModelBindingContext bindingContext)
{
return true;
}
}
I recommend you to follow this article and test and convert to your case. 我建议你遵循这个文章和测试,并转换成你的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.