2

Sorry all , my English isn't very well , so I will try to use code to tell everyone what I need.In format situations,I will create 2 controller when I have 2 models**(ex: users/product)** , as following

var serializer = new JavaScriptSerializer();
var users= new users() { id = 1};
var jsonText = serializer.Serialize(users);
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:65370/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
StringContent content = new StringContent(jsonText, Encoding.UTF8, "application/json");

///////////////

var a = client.PostAsync("api/users", users, new JsonMediaTypeFormatter()).Result; (client)  
var b = client.PostAsync("api/product", product, new JsonMediaTypeFormatter()).Result; (client)  

and then when the users and product controllers was created the post code should be like as following

public IHttpActionResult Postusers(users aa) {} (server)
public IHttpActionResult Postproduct(product bb) {} (server)

I just want to create 1 controller for above like as follwing

var b = client.PostAsync<users/product>("api/all", product, new JsonMediaTypeFormatter()).Result;(client)

public IHttpActionResult Post<T>(Object ForAll) where T : new() {} (server)

how can i building this structure,please help me to do this serious question, thanks

1 Answer 1

1

You can have a generic base controller like this:

public abstract class BaseController<T> : ApiController
    where T : class
{
    public virtual IHttpActionResult Post(T obj)
    {
        // ......
    }

    // some other common methods
}

and then you inherit from that controller for each of your entities:

public class UsersController : BaseController<User>
{}

public class ProductsController : BaseController<Product>
{}

this way you can keep your conventions, so "api/users" and "api/products" routes will work and the common code is refactored to a generic method.

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

6 Comments

Hi William ,thankyou so much for your help , this is a great way ,however , for your code on Controller , it is one controller in the ***.cs , or 2 controllers,PS I hope it is just needed 1 Controller to deal with all of controllers ,this is a possible or not ?? thanks again
Sorry William , I mean can i setup one ***Controller for all , if I have 100 controllers , i have to write 100 functions in the the *.cs . thanks
If you want only one controller you can use some convention for the routing and use reflection to activate the controllers but it will be more complicated.
thanks again, do you have any references or information to give me for research,i hope i can do it ,if you do mind .
you can have a controller that takes the name of the resource as a string so when you call /api/users it receives users as a string, then (using reflection) obtain the type User and then activate the generic controller (using reflection again) with User
|

Your Answer

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