0

Is there a quick and easy way to detect which environment your app is running on and modify the html accordingly?

I would like to change my shared layout view to give the header a red background when it is running on my test server.

I'd like to avoid writing an IIS module or HTTP response filter if possible.

woggles
  • 7,444
  • 12
  • 70
  • 130
  • I don't really agree with the "how to detect that the site is running in debug" duplicate, as this will mean you can put a release build on your test server and not see any difference. – CodeCaster Jun 13 '15 at 11:23
  • @CodeCaster yeah - good point > I didn't really want to deploy to the test site in debug mode – woggles Jun 13 '15 at 11:24
  • Can you do an URL check in the layout file and append a style sheet accordingly. Something like `Request.URL.Contains("debugurl")` – scgough Jun 13 '15 at 11:39
  • How come this was closed as dupe with only two votes. The second one being community? – Martin Smith Jun 13 '15 at 11:55
  • Guess that must be it. Not sure why it wouldn't show your name then. – Martin Smith Jun 13 '15 at 12:19

1 Answers1

1

You can use a filter attribute that runs for every action method and set a property in every view's ViewBag, for example by reading the environment from the web.config. Where "it" being an appSettings key like "environment", or a list of hostname and color mappings.

See How to set ViewBag properties for all Views without using a base class for Controllers? and especially Mirak's answer.

Then in your layout, use this ViewBag property to set the HTML style or class.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272