I feel fairly confident that I have this wrong (or backwards). I'm looking to determine what environment a request is coming from, so that I can determine what database to access (based on the referrer url).
The end point is a "self-hosted" API on one of our servers, and I'd like to send requests from both development and production to the service, and handle the requests accordingly. I'd like to avoid adding additional frameworks, as well as avoiding having to handle the environment in each action call.
Here's what I have going so far.
public ServiceProvider svc;
public ECTaskService lab;
public BaseAPIController()
{
//I WAS HOPING FOR THE ENVIRONMENT REFERENCE HERE....
svc = new ServiceProvider();
lab = new ECTaskService();
}
My hope was that I could access the filter context below from the constructor above to pass the environment down to the service provider, which would handle the all of the facets of the database / dev environment as needed.
public class LabelServerFilter : ActionFilterAttribute {
public override void OnActionExecuting(HttpActionContext actionContext)
{
Environment(actionContext);
base.OnActionExecuting(actionContext);
}
public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
Environment(actionContext);
return base.OnActionExecutingAsync(actionContext, cancellationToken);
}
public string Environment(HttpActionContext actionContext)
{
try
{
var env = "PRODUCTION";
Uri referrer = actionContext.Request.Headers.Referrer;
if (referrer != null)
{
string clientDomain = referrer.GetLeftPart(UriPartial.Authority);
if (clientDomain.IndexOf("myinternalurldev") > -1)
{
env = "DEVELOPMENT";
}
}
//Console.Write("[" + env + "]");
return env;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ex);
}
return "PRODUCTION";
}
}
Thanks in advance!