asp.net mvc如何将发布数据转换为模型?(How does asp.net mvc convert post data to a model?)

编程入门 行业动态 更新时间:2024-10-18 08:27:28
asp.net mvc如何将发布数据转换为模型?(How does asp.net mvc convert post data to a model?)

我正在从MVC区域之外的项目中提取一些代码到HttpHandler,同时重用尽可能多的代码。

为了方便起见,我想将发布的数据转换为已在Controller中使用的相同模型。

所以,现在我正在寻找逻辑MVC在封面下使用神奇地将发布的数据转换为模型,但经过几次反编译尝试后我无法找到它。

有人能指出我正确的方向吗?

谢谢!

SaphuA:基本上,我想输入HttpContext和目标Model的类型,并让helper方法执行其余的映射。

I am extracting some code from a project outside of the MVC-area to a HttpHandler while reusing as much code as possible.

To make things easy I would like to convert the posted data to the same Model that was already used in the Controller.

So, now I am looking for the logic MVC is using under the covers to magically convert posted data to a Model, but after several decompile attempts I am unable to find this.

Could anyone point me in the right direction?

Thanks!

SaphuA: Basically, I want to input the HttpContext and the type of the target Model and have the helper method do the rest of the mapping.

最满意答案

正如Eric所说,只是扩展DefaultModelBinder.cs

默认模型绑定器在这里

从控制器动作调用程序调用模型绑定器

这里总体流程很好地概述了,所以只需滚动查看控制器动作调用程序的调用位置。

如果您想围绕绑定器的实现启动一些单元测试,请查看MVC源测试

使用github镜像,因为codeplex不支持行号链接:-)

HTH

As Eric says, just extent the DefaultModelBinder.cs

The default model binder is here

The model binder is called from the controller action invoker

The overall flow is nicely outlined here so just scroll to check where the controller action invoker is called.

If you want to spin up some unit tests around your implementation of the binder, check out the MVC source tests

Using the github mirror because codeplex does not support line number linking yet :-)

HTH

更多推荐

本文发布于:2023-04-28 03:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   模型   数据   mvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!