简体   繁体   English

如何通过uri或http正文将复杂对象的实例传递给asp.net Web API控制器而没有任何参数?

[英]How to pass instance of complex object via uri or http body without any params to asp.net web api controller?

I want to create instance of some type based on selected value, so I pass this type's name as a string via url using jquery.ajax() method to web api controller method, where I create instance of this type with reflection. 我想根据选定的值创建某种类型的实例,因此我使用jquery.ajax()方法将这种类型的名称作为字符串通过url传递给Web api控制器方法,在该方法中,我使用反射来创建这种类型的实例。

But I'm conserned about this approach. 但是我对此方法很了解。

I know that I can implement ModelBinding with custom TypeConverter or [FromUri] attribute. 我知道我可以使用自定义TypeConverter[FromUri]属性来实现ModelBinding And pass some data to initialize object fields. 并传递一些数据来初始化对象字段。

The problem is that my type has default empty constructor and it doesn't need any fields to be initialized. 问题是我的类型具有默认的空构造函数,并且不需要初始化任何字段。

How can I pass instance of complex object via url or http body, without specifying any parameters? 如何在不指定任何参数的情况下通过url或http正文传递复杂对象的实例?

You are right you need to implement you own custom model binder, which will call 没错,您需要实现自己的自定义模型联编程序,该联编程序将调用
Activator.CreateInstance. Activator.CreateInstance。 Then you need to apply the MoldeBindingAttribute to your action. 然后,您需要将MoldeBindingAttribute应用于您的操作。 In the action itself you need to declare a parameter of type object(because you don't know the type at compilation time). 在动作本身中,您需要声明一个object类型的参数(因为在编译时您不知道类型)。 That is the only way to achive your requirements. 这是达到您的要求的唯一方法。

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

相关问题 如何将复杂的JSON对象传递给ASP.net控制器 - How to pass complex JSON object to ASP.net controller 如何在ASP.NET Web API中获取uri的请求对象? - How to get the request object for uri in ASP.NET Web API? ASP.NET WEB API 2中的复杂对象 - Complex object in ASP.NET WEB API 2 具有通用参数的 ASP.NET Core Web API 控制器 - ASP.NET Core Web API controller with generic params 通过InjectionConstructor传递asp.net网页的当前实例 - pass current instance of asp.net web page via InjectionConstructor 如何使用POST将复杂对象从视图传递到控制器-ASP.NET MVC - How can i pass complex object from view to controller with POST - ASP.NET MVC 将$ http.get()方法中的$ scope.object传递给asp.net web api - pass $scope.object in $http.get() method to asp.net web api 如何从 ASP.NET Web API ValueProvider 中的 HTTP POST 请求检索正文值? - How do I retrieve body values from an HTTP POST request in an ASP.NET Web API ValueProvider? 如何从ASP.NET Web APi中的Body传递参数信息? - how to pass Parameters Info from the Body in ASP.NET Web APi? 如何将ASP.NET Web API属性路由与复杂对象参数一起使用? - How do I use ASP.NET Web API Attribute Routing with a complex object parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM