asp.net web api - How to read header in controller constructor -
is possible read header authorization
in controller constructor? , how? , best solution (reading header value in controller constructor) achieve goal ?
the following code of classes inherit interface:
public interface iprovider { string senddata(string data); //other methods } public class firstprovider : iprovider { private string _url; public firstprovider(string url) { _url = url; } public string senddata(string data) { //send data first provider website (_url) //return result } } public class secondprovider : iprovider { //some code }
and following api controller:
public class providercontroller : apicontroller { private iprovider _provider; public providercontroller() { //read authorization key headers (how??) string authtoken; //fetch database/cache provider use based on authorization key var provider = providers.getprovider(authtoken); //initialize _provider based on database query result if (provider.name == "a") { _provider = new firstprovider(provider.data); } else if (provider.name == "b") { _provider = new secondprovider(provider.data); } //other providers added in future } public providercontroller(iprovider provider) { _provider = provider; } public ihttpactionresult senddata(string data) { _provider.senddata(data); return ok(); } }
you use system.web.httpcontext.current.request.headers
however, should use constructor injection (which seem going towards other constructor public providercontroller(iprovider provider)
you use ioc container conditional binding based on request. see ninject injection based on route data value
Comments
Post a Comment