简体   繁体   English

在ASP.NET MVC中查询字符串到C#对象

[英]Query String to C# Object in ASP.NET MVC

I have a very generic question. 我有一个非常通用的问题。

When Developing with ASP.NET MVC. 使用ASP.NET MVC进行开发时。 It provides a great way to utilize C# to interact with Client side objects like Query string. 它提供了一种利用C#与客户端对象(如Query字符串)进行交互的好方法。

I wanted to know how does ASP.NET MVC Framework converts Query String sent in GET request or Form Data in POST request to a Strongly Typed C# class? 我想知道ASP.NET MVC Framework如何将在GET请求中发送的查询字符串或POST请求中的表单数据转换为强类型的C#类? Are there any specific HttpHandler or HttpModules for this? 这有什么特定的HttpHandler或HttpModules吗? Or are they using any Pre-Defined Action Filter? 或者他们使用任何预定义的动作过滤器?

I want to implement similar feature in WCF based RESTFull services. 我想在基于WCF的RESTFull服务中实现类似的功能。 I believe I will need to use Interceptor for this purpose. 我相信我需要为此目的使用Interceptor。

Please provide some advice. 请提供一些建议。

Thanks in Advance. 提前致谢。

You're referring to ASP.NET MVC Model Binding. 您指的是ASP.NET MVC模型绑定。 This is actually a completely separate part of the framework than ActionFilters or HttpModules. 这实际上是一个完全独立的框架部分,而不是ActionFilters或HttpModules。 It works by examining "value providers" - the sources of information in the request, like form data, the query string, or even the route - and attempting to match them by name to properties in the complex type in your controller action. 它的工作原理是检查“值提供者” - 请求中的信息源,如表单数据,查询字符串,甚至路由 - 并尝试按名称将它们与控制器操作中复杂类型的属性进行匹配。

As Kenneth mentioned, you can check out the source on CodePlex , but if you're looking to write services using RESTful architectural patterns you can start using the Web API features in the MVC4 beta, which also supports model binding (so you won't have to reinvent it!) 正如Kenneth所提到的,您可以查看CodePlex上的源代码,但如果您希望使用RESTful架构模式编写服务,则可以开始使用MVC4 beta中的Web API功能,该功能还支持模型绑定(因此您不会不得不重新发明它!)

It's also worth mentioning that the Web API was being developed for WCF but is now a part of ASP.NET MVC , which makes sense since ASP.NET MVC, like the Web API, are both built primarily around HTTP. 值得一提的是,Web API是为WCF开发的,但现在是ASP.NET MVC的一部分,这是有道理的,因为ASP.NET MVC(如Web API)主要是围绕HTTP构建的。

I'm not too familiar with the wcf extensibility model, but on the mvc3 side, all the source is out there. 我对wcf扩展性模型不太熟悉,但在mvc3方面,所有的源都在那里。 You can see exactly how Model binding is implemented. 您可以确切地了解如何实现模型绑定。 Just download the source, its a rather informative read. 只需下载源代码,这是一个相当翔实的阅读。

http://aspnet.codeplex.com/releases/view/58781 http://aspnet.codeplex.com/releases/view/58781

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM