0

I have two mvc applications with cshtml views, both with a different url. They are acceptatance and production apps. I need to alter/hide some text on the homeview depending on the url. So for example: If the URL contains the word "production" i need to add the text "PRODUCTION" to a div on the homescreen.

Can anyone point me in the right direction?

Kara
  • 6,115
  • 16
  • 50
  • 57
JVGBI
  • 565
  • 1
  • 8
  • 26
  • http://stackoverflow.com/questions/4597050/how-to-check-if-the-url-contains-a-given-string you may use this answers as your div element. – alphandinc Mar 09 '17 at 10:48
  • if your only option is to read from URL then read the Request.Uri in your controller action and pass your environment name to your view. Ideally, you should either have a config setting for such things. – KKS Mar 09 '17 at 11:08

2 Answers2

2

Use ViewBags. Declare ViewBag in controller for example:

ViewBag.Type = "Production"

on production page, and:

ViewBag.Type = "Other Type"

in other method in controller. Then in homeview check what is under this ViewBag.

if (!string.IsNullOrEmpty(ViewBag.Type))
{
    if (ViewBag.Type == "Production")
    {
        <p>Production</p>
    }
    else if (ViewBag.Type == "Other Type")
    {
        <p>Other Type</p>
    }
}
1

There is better way of doing this.

you could use web.config transform that way you will have different config files for test and production.

in web.config create appsetting

<appSettings>
            <add key="Environment" value="Test" />
</appSettings>

in cshtml check key value and do stuff

    @if(!string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("Environment")) && ConfigurationManager.AppSettings.Get("Environment") == "Test")
{

}
jjj
  • 1,136
  • 3
  • 18
  • 28