Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mikkel Johansen 116 posts 292 karma points
    Jan 27, 2015 @ 12:12
    Mikkel Johansen
    0

    Checking for isLocal in ApplicationStarted

    Umbraco 7

    Is it possible to check in "ApplicationStarted" if the site is running on localhost?

    Something like this

    public class BookEvents : ApplicationEventHandler {
      protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
      {
        if (isLocal)
        {
          // Do some stuff only if on localhost
        }
        else
        {
           // Only if NOT localhost
        }
      }
    }     
  • Jesper Hauge 298 posts 487 karma points c-trib
    Jan 27, 2015 @ 12:42
    Jesper Hauge
    0

    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

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 27, 2015 @ 12:49
    Sebastiaan Janssen
    100

    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 Johansen 116 posts 292 karma points
    Jan 27, 2015 @ 12:52
    Mikkel Johansen
    0

    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

  • Jesper Hauge 298 posts 487 karma points c-trib
    Jan 27, 2015 @ 13:13
    Jesper Hauge
    0

    @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

  • Mikkel Johansen 116 posts 292 karma points
    Jan 27, 2015 @ 13:50
    Mikkel Johansen
    0

    Tanks Jesper and Sebastiaan - I'll go with the web.config :-)

    - Mikkel

Please Sign in or register to post replies

Write your reply to:

Draft