1

Is it possible that I can have ActionResult/JsonResult which can accept a generic type of parameter? i.e. I want to do something like this:

public class mycontroller : Controller
{
    public async Task<JsonResult> Add(T type)
    {

    }
}

where T can be any class which I have defined in my model. Any thoughts on this?

2 Answers 2

4

No, this is not possible. In order to call an action based on a request body, there's what's referred to as the modelbinder, that figures out how the data in the request should be constructed to fit the parameters of the action method. Without a concrete type to work with, the modelbinder has no guidance.

UPDATE

Just to explain a little better. Typically when dealing with generics, you must specify the type either explicitly (using the <Type> syntax when calling the method) or implicitly (by passing in an instance of something for a generic parameter, you're saying that the generic type is the type of that instance).

However, because of the way an action is called, there is no way to do either. The action cannot be called until MVC can determine what it should pass as parameters to it, and it cannot do that without inspecting the request for something that will work. In order to determine if there's something in the request that can be used as a parameter, MVC must know what kind of thing needs to be passed into the action first. That is why you cannot have a generic action. Without MVC knowing before hand what type(s) it's dealing with, it can't figure out what to do with the request.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure what confusion you're talking about. It's pretty straight-forward. You're wanting to have a generic parameter on an action, and it is not possible to do that. Plain and simple.
2

MVC's model binder needs to map incoming form fields (or JSON or XML data or whatever) to properties on the type of your method parameters.

In other words, an incoming { "foo" : "bar" } requires Bar in public async Task<JsonResult> Add(Bar bar) to have a foo property.

WebAPI doesn't know what types have a foo property. Yes, it could scan all assemblies in the bin directory and find the "most appropriate" type, but given the example JSON above and two classes with a foo property, which is "most appropriate" and why?

You cannot answer that question and neither did the MVC team want to, so no, your WebAPI/MVC controller methods must work with concrete parameter types.

2 Comments

never give negative rating to anyone for asking question. to ask a question is right of a person who do not know. anyway thanks,
if you think you are right, go for it, my concern was to ask you guys and thanks for your reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.