Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different url based on development environment with C# MVC

Tags:

c#

asp.net-mvc

I have two MVC web applications, a www.company.com and a solution.company.com

The website www.company.com include links to solution.company.com/Contact but how can I set the href in the View to be able to test them in the development/pre-production/production environments?

Dev:

<a href="http://localhost:88/Contact/">Contact Us</a> 

QA:

<a href="http://qa.solution.company.com/Contact/">Contact Us</a> 

PRD:

<a href="http://solution.company.com/Contact/">Contact Us</a> 
like image 814
Patrick Avatar asked Oct 17 '17 21:10

Patrick


2 Answers

You can use web.config to set different variables. Use separate web.config for each environment. Eg. web.release and web.debug. Same way you can use. Separate files for each environment

If you use, Octopus deployment. Use can set in octopus variables also.

<appSettings>
  <add key="MyVariable1" value="False" />
  <add key="MyName" value="akshay bheda" />
</appSetting>

Now you can easily access this variable and its value from your C# code:

string myVariable = System.Configuration.ConfigurationSettings.AppSettings["MyName"];

Now instead of writing url there, you can use this string instead.

<a href="<%= ConfigurationManager.AppSettings["someKey"] %>">

If you don't want to use Octopus Deployment, You can follow below steps.

1.) Create new Configuration from Configuration Manager. It is located under Build Menu. Configuration Manager and create a new configuration say for eg. Production and select Copy settings from Debug or any other present web.config so you don't have to write again.

2.) After creating a new configuration, Right click on Web.config and click Add Config Transformation Web.config settings after that you will find your new configuration's web.config.

Make the changes in the appSettings section in each of your config and while starting the project, select your build configuration.

It will take the configuration settings from your appSettings section from that respective config.

like image 110
Akshay Bheda Avatar answered Sep 18 '22 01:09

Akshay Bheda


If for some reason you do not have an automated deploy system that lets you specify per-environment variables (like Octopus as mentioned in previous answers) there are a couple of other options.

1: Put your settings in a machine.config file for each environment and deploy that config to the appropriate servers.

2: Put your relationships in your config file:

  <appSettings>
    <add key="contact-for-localhost" value="http://localhost:88/Contact/" />
    <add key="contact-for-qa.company.com" value="http://qa.solution.company.com/Contact/" />
    <add key="contact-for-www.company.com" value="http://solution.company.com/Contact/" />
    <add key="contact-for-company.com" value="http://solution.company.com/Contact/" />
  </appSettings>

then ask for the settings by hostname in your controller:

var contactLink = ConfigurationManager.AppSettings[$"contact-for{Request.Url.Host}"]

and pass it to your model.

As you don't have different configurations to switch between, to test this approach you can change your hosts file (c:\System32\drivers\etc\hosts) to fake being each of the environments

127.0.0.1  localhost
127.0.0.1  qa.company.com
127.0.0.1  company.com
127.0.0.1  www.company.com

Then comment out or remove the non-localhost entries to connect to the real servers.

like image 43
Malcolm Avatar answered Sep 22 '22 01:09

Malcolm