如何构建一个自定义模型绑定器,它将根据请求上下文返回不同类型的模型?
我收到了针对特定操作的传入请求(来自 Facebook 的积分处理),该操作将具有不同的内容,因此我有不同的模型类来处理该请求。
这是我的动作:
public ActionResult Index([ModelBinder(typeof(FacebookCreditModelBinder))] IFacebookRequest facebookRequest)
{
if (facebookRequest is FacebookPaymentsGetItemsRequest)
{
// do whatever
}
}
这是我的模型活页夹。
public class FacebookCreditModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var binder = new DefaultModelBinder();
// how to change the model here in the bindingContext?
return binder.BindModel(controllerContext, bindingContext);
}
}
例如,如果传入的 var“method”是“ payments_get_items”,我想创建一个 FacebookPaymentsGetItemsRequest
对象,如果方法是“ payments_status_update”,我想创建一个 FacebookPaymentsStatusUpdateRequest
对象,我不知道如何更改绑定上下文中模型的类型。 是否可以更改自定义模型绑定器中模型的类型?
其他方法:我也用 BindModel 尝试过,我能够返回正确的对象,但所有属性均为 null,因为它不是由默认模型绑定器填充的:
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
NameValueCollection form = controllerContext.HttpContext.Request.Form;
if (form.Get("method") == "payments_get_items")
{
return new FacebookPaymentsGetItemsRequest();
}
...
I have incoming requests (from Facebook for Credits handling) on a specific action which will have have different contents, so I have different model classes to handle that.
This is my action:
public ActionResult Index([ModelBinder(typeof(FacebookCreditModelBinder))] IFacebookRequest facebookRequest)
{
if (facebookRequest is FacebookPaymentsGetItemsRequest)
{
// do whatever
}
}
And this is my model binder.
public class FacebookCreditModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var binder = new DefaultModelBinder();
// how to change the model here in the bindingContext?
return binder.BindModel(controllerContext, bindingContext);
}
}
I want to create for example a FacebookPaymentsGetItemsRequest
object if the incoming var "method" is "payments_get_items" and a FacebookPaymentsStatusUpdateRequest
if method is "payments_status_update" and I don't know how to change the type of the model in the bindingContext.
Is it possible to change the type of the model in a custom model binder?
Other approach: I tried it with BindModel also and I'm able to return the correct object but all properties are null because it is not filled by the default model binder:
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
NameValueCollection form = controllerContext.HttpContext.Request.Form;
if (form.Get("method") == "payments_get_items")
{
return new FacebookPaymentsGetItemsRequest();
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
并在
Application_Start
中注册:然后您的控制器操作可能如下所示:
You could do this:
and register in
Application_Start
:Then your controller action could look like this:
.NET Core 中似乎不存在
DefaultModelBinder
。网络上的其他解决方案使用过时的类型ComplexTypeModelBinder
。其中一些人没有填写非共享字段。我花了几个小时尝试一些东西。这是我的简单解决方案。DefaultModelBinder
seems absent in .NET Core. Other solutions on the web use the obsolete typeComplexTypeModelBinder
. Some of them were not filling out not-shared fields. I spent several hours trying out stuff. So here's my simple solution.