How to create a dymanic function in ASP.NET MVC with Web API and EF -
sorry , english isn't , try use code tell need.in format situations,i create 2 controller when have 2 models**(ex: users/product)** , 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 = client.postasync("api/users", users, new jsonmediatypeformatter()).result; (client) var b = client.postasync("api/product", product, new jsonmediatypeformatter()).result; (client)
and when users , product controllers created post code should following
public ihttpactionresult postusers(users aa) {} (server)
public ihttpactionresult postproduct(product bb) {} (server)
i want create 1 controller above follwing
var b = client.postasync<users/product>("api/all", product, new jsonmediatypeformatter()).result;(client) public ihttpactionresult post<t>(object forall) t : new() {} (server)
how can building structure,please me serious question,
you can have generic base controller this:
public abstract class basecontroller<t> : apicontroller t : class { public virtual ihttpactionresult post(t obj) { // ...... } // other common methods }
and inherit controller each of entities:
public class userscontroller : basecontroller<user> {} public class productscontroller : basecontroller<product> {}
this way can keep conventions, "api/users"
, "api/products"
routes work , common code refactored generic method.
Comments
Post a Comment