Various URLs Based on C # MVC Development Environment

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

The website www.company.com includes links to solution.company.com/Contact, but how can I install href in a view so that I can test them in a development / preparation / production environment?

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> 
+7
c # asp.net-mvc
source share
8 answers

You can use web.config to set different variables. Use a separate web.config for each environment. For example. web.release and web.debug. The same can be used. Separate files for each environment

If you are using Octopus deployment. Use can also be set in variable octopuses.

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

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

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

Now instead of writing url you can use this line.

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

If you do not want to use Octopus Deployment, you can follow these steps.

1.) Create a new configuration from Configuration Manager. It is located in the Build menu. Configuration manager and create a new configuration, for example, for example. production and select "Copy settings from Debug" or any other existing web.config so you don't have to write again.

2.) After creating a new configuration, right-click on Web.config and select Add Configuration Configuration . Web.config Settings after that you will find the new web.config configuration.

Make changes to the appSettings section in each of your configurations, and when you start the project, select the assembly configuration.

From this configuration, select configuration settings from the appSettings section.

+7
source share

Using the <appSettings> in web.config, you can maintain a file attribute that will load the external configuration with its own set of keys / values. They will override any settings that you have in your web.config or add to them.

eg,

 <appSettings file=".\EnvironmentSpecificConfigurations\dev.config"> <appSettings file=".\EnvironmentSpecificConfigurations\qa.config"> <appSettings file=".\EnvironmentSpecificConfigurations\prod.config"> 

Inside the .config file

  <appSettings> <add key="webaddress" value="yourwebsite.com"/> </appSettings> 

Aspx Page

 <a href="<%=ConfigurationManager.AppSettings["webAddress"]%>">Lβ€Œβ€‹ink</a> 
+7
source share

If for some reason you do not have an automated deployment system that allows you to specify variables for each environment (for example, Octopus, as indicated in previous answers), there are a couple of other options.

1: Put your settings in the machine.config file for each environment and install this config in the appropriate servers.

2: Put your relationship in your configuration 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.

Since you do not have different configurations for switching between them, to test this approach, you can modify the hosts file (c: \ System32 \ drivers \ etc \ hosts) to fake yourself in each environment

 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 delete non-localhost entries to connect to real servers.

+1
source share

From what you see in already existing answers, you will need to modify the static hrefs line for what is defined at compile time. I think you should follow the previous recommendations and write different tags for Web.Debug.config and Web.Release.config , but not to solve a specific situation, mainly to determine your connection strings, if you have different database sources for each base.

To solve your specific situation, just use the MVC Url helper and replace your static hrefs with something like <a href="@Url.Content("~/Contact")"> Contact </a> , where "~/" means your domain root, no matter what it is.

Also, if you did not specify the controller name before "Contact", I assume that you have a ContactController in your application and reaches its index pointer with that address.

0
source share

What if you made a model for MVC View a Dictionary or added a settings dictionary to the returned model and just returned data from the controller?

 <a href="@Model["ContactUsUrl"]">Contact Us</a> 

OR

 <a href="@Model.Settings["ContactUsUrl"]">Contact Us</a> 

Alternatively, you can simply use the model with the ContactUsUrl property

 <a href="@Model.ContactUsUrl">Contact Us</a> 

Then, in your controller code, you can use any logic that you want to solve, be it DEV, QA or PROD, and pull the data for the URL from the appropriate source, for example. database, configuration file, custom settings file. This abstracts the logic and makes it easier to use your data on the page.

This is an alternative to using web.config directly.

0
source share

The proposed solutions are good, but a lot of overhead is just for switching a single IMHO URL.

I would take the environment variable approach and set the URL accordingly.

Contact.cshtml

 @{ var env = Environment.GetEnvironmentVariable("MY_APP_ENV"); var url = env == "prod" ? "http://qa.solution.company.com/Contact/" : env == "qa" ? "http://solution.company.com/Contact/" : "http://localhost:88/contact/"; } <a href="@url">Contact Us</a> 

If this is a large project and you can spend some time, I would put it in appSettings and configure the .xml parameters for each environment and use it during deployment.

0
source share

If you want to use DotNet Core or want to switch to it, then it is best to implement its environment customization capabilities.

Documentation on the official website: Working with multiple environments and configuration in the ASP.NET core

Here is a good example.

 var builder = new ConfigurationBuilder() SetBasePath(hostEnv.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true); 

All you need to do is define 3 different appsettings . And set ASPNETCORE_ENVIRONMENT on your system where you want to run your tests or development.

Or you can use env.IsEnvironment("environmentname") to check something at runtime.

0
source share

You do not need to use the application settings for this. Just use the URLHelper this part of MVC and it will provide the correct hostname.

So instead:

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

You do it:

 <a href="@Url.Action("Index", "Contact")">Contact Us</a> 

This will create a link that points to the Contact action in the IndexController .

Another option is to use the HTMLHelper extension method to generate the entire tag for you. In this case, you will do the following:

 @Html.ActionLink("Index", "Contact", "Contact Us") 
-one
source share

All Articles