ModelBinder的未调用 [英] ModelBinder is not invoked

查看:160
本文介绍了ModelBinder的未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

previous问题,我实现了一个映射 / API / V1 /部件/ 1,2,3

Per my previous question, I implemented a model binder that maps /api/v1/widgets/1,2,3 to

// WidgetsController.cs: 
public ActionResult Show(IEnumerable<int> idArgs)
{

}

这是工作了一段时间,但现在是没有任何更长的时间。 我ModelBinder的甚至没有被所有调用。:当我的行为被调用时, idArgs 的空列表的价值,即使我设置其在路由,这表明,我认为默认的模型绑定认为它是从什么地方得到的值默认值。自上周以来,我做的时候,它正在唯一的变化是,previously,我呼吁我的行动 ShowMany。从那以后,我把它重命名为显示。谁能帮我弄清楚为什么没有被调用我的ModelBinder的?

This was working for a while, but now it is not any longer. My ModelBinder is not even being invoked at all. When my action is invoked, idArgs has a value of the empty list, even if I set its default value to null in the route, which suggests to me that the default model binder thinks it's getting a value from somewhere. The only change I've made since last week when it was working is that previously, I had called my action ShowMany. Since then, I renamed it to Show. Can anyone help me figure out why my ModelBinder is not being invoked?

在的global.asax.cs,我有

In global.asax.cs, I have

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

    ModelBinders.Binders.Add(typeof(IEnumerable<int>), new IEnumerableOfIntCSVModelBinder());
}

和路线看起来像这样(我已经验证正在使用这条路线):

And the route looks like this (I have verified that this route is being used):

context.MapRoute(
    "show",
    "api/{controller}/{idArgs}",
    new { action = "show" },
    new { httpMethod = new HttpMethodConstraint("GET"), idArgs = @"^(\d+,)+\d+,?$" }
);


编辑:我试图搞乱一些与搞乱航线,以及注释掉 JsonValueProvider ,我依然得到一个空数组。在我的控制器,我可以做


I've tried messing messing with the routes some more, as well as commenting out the JsonValueProvider, and I'm still getting an empty array. In my controller, I can do

var ids = RouteData.Values["idArgs"];

和得到字符串1,2,3。如果只有框架将它传递给我的ModelBinder的,我会ModelBinder的把它变成了的IEnumerable

and get the string "1,2,3". If only the framework would pass this to my ModelBinder, my ModelBinder would turn it into the IEnumerable.

我使用AutoFac。有没有可能是AutoFac注入空数组到我的控制器的方法?我已经没有问题,这样在其他地方(我们在这个项目中使用AutoFac无处不在。)

I am using AutoFac. Is it possible that AutoFac is injecting an empty array into my controller method? I haven't had problems like this in other places (and we use AutoFac everywhere in this project.)

EDIT2 :我也试过装潢均 idArgs 操作参数,以及与控制器 [ModelBinder的(typeof运算( IEnumerableOfIntCSVModelBinder))] ,但这没有影响。

Edit2: I also tried decorating both the idArgs action parameter, and the controller with [ModelBinder(typeof(IEnumerableOfIntCSVModelBinder))], but this had no effect.

推荐答案

我看到你有一个 JsonValueProviderFactory 中加入你的的Application_Start 。也许有东西在这个工厂$ P $的实施pvents被击中模型绑定?

I see that you have a JsonValueProviderFactory added in your Application_Start. Maybe there is something in the implementation of this factory that prevents the model binder from being hit?

另外你已经证明了网址 / API / V1 /部件/ 1,2,3 没有任何关系的路由定义,你有 RESTAPI / {控制器} / {} idArgs

Also the url you have shown /api/v1/widgets/1,2,3 has no relation to the route definition that you have "restapi/{controller}/{idArgs}".

这篇关于ModelBinder的未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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