Is it possible to check in "ApplicationStarted" if the site is running on localhost?
Something like this
publicclassBookEvents : ApplicationEventHandler {
protectedoverridevoid ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
if (isLocal)
{
// Do some stuff only if on localhost
}
else
{
// Only if NOT localhost
}
}
}
Isn't "localhost" just a build in a-record for 127.0.0.1?
Which means you cannot say that an application is running "on localhost", it always is. (If you've got IIS running on your machine going to localhost in your browser will display web site registered as default website in IIS, both on a server and on a normal windows machine) Sometimes the application runs in IIS, and is configured to answer on external domains as well.
So I'd guess you need to look at the request coming in, not the application.
I would just take the pragmatic approach: put a key in your web.config's appSettings (for example: <add key="environment" value="local" /> and during deploys do a config transform to set it to "staging" or "live". In ApplicationStarted you can read it and do your conditional logic.
You may be able to find a service that can do a lookup for a domain and if the lookup gives no results you can assume it's a local IP but that seems a bit dodgy.
@Mikkel - that's because there is no request in ApplicationStarted, it's an event handler that runs during application startup. In principle you cannot know anything about localhost vs. domain until you see a request, and the request cannot be processed until the application has finished starting up.
I'd go with Sebastiaans proposal (in fact we already do that in several of our projects): Set a value in appSettings that'll tell you about the environment, and have different web config files, when running locally and on destination servers. It's entirely possible to read appSettings in a ApplicationStarted event.
Checking for isLocal in ApplicationStarted
Umbraco 7
Is it possible to check in "ApplicationStarted" if the site is running on localhost?
Something like this
Isn't "localhost" just a build in a-record for 127.0.0.1?
Which means you cannot say that an application is running "on localhost", it always is. (If you've got IIS running on your machine going to localhost in your browser will display web site registered as default website in IIS, both on a server and on a normal windows machine) Sometimes the application runs in IIS, and is configured to answer on external domains as well.
So I'd guess you need to look at the request coming in, not the application.
.Jesper
I would just take the pragmatic approach: put a key in your web.config's appSettings (for example:
<add key="environment" value="local" />
and during deploys do a config transform to set it to "staging" or "live". In ApplicationStarted you can read it and do your conditional logic.You may be able to find a service that can do a lookup for a domain and if the lookup gives no results you can assume it's a local IP but that seems a bit dodgy.
The problem is that I have no access to "Request" in the ApplicationStarted.
I tought that "umbracoApplication.Request" could help me, but that is empty.
-Mikkel
@Mikkel - that's because there is no request in ApplicationStarted, it's an event handler that runs during application startup. In principle you cannot know anything about localhost vs. domain until you see a request, and the request cannot be processed until the application has finished starting up.
I'd go with Sebastiaans proposal (in fact we already do that in several of our projects): Set a value in appSettings that'll tell you about the environment, and have different web config files, when running locally and on destination servers. It's entirely possible to read appSettings in a ApplicationStarted event.
.Jesper
Tanks Jesper and Sebastiaan - I'll go with the web.config :-)
- Mikkel
is working on a reply...