简体   繁体   中英

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.

But I'm conserned about this approach.

I know that I can implement ModelBinding with custom TypeConverter or [FromUri] attribute. 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?

You are right you need to implement you own custom model binder, which will call
Activator.CreateInstance. Then you need to apply the MoldeBindingAttribute to your action. In the action itself you need to declare a parameter of type object(because you don't know the type at compilation time). That is the only way to achive your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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