简体   繁体   English

基于C#MVC开发环境的不同url

[英]Different url based on development environment with C# MVC

I have two MVC web applications, a www.company.com and a solution.company.com我有两个 MVC Web 应用程序,一个 www.company.com 和一个 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?网站 www.company.com 包含指向 solution.company.com/Contact 的链接,但如何在视图中设置 href 以便能够在开发/预生产/生产环境中测试它们?

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> 

You can use web.config to set different variables. 您可以使用web.config设置不同的变量。 Use separate web.config for each environment. 为每个环境使用单独的web.config。 Eg. 例如。 web.release and web.debug. web.release和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: 现在,您可以从C#代码轻松访问此变量及其值:

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

Now instead of writing url there, you can use this string instead. 现在不用在那里写url,而是可以使用这个字符串。

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

If you don't want to use Octopus Deployment, You can follow below steps. 如果您不想使用Octopus Deployment,可以按照以下步骤操作。

1.) Create new Configuration from Configuration Manager. 1.)从Configuration Manager创建新配置。 It is located under Build Menu. 它位于“构建菜单”下。 配置管理器 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. 生成并选择从Debug或任何其他当前web.config复制设置,这样您就不必再次写入。

2.) After creating a new configuration, Right click on Web.config and click Add Config Transformation 2.)创建新配置后,右键单击Web.config并单击Add Config Transformation Web.config设置 after that you will find your new configuration's web.config. 之后,您将找到新配置的web.config。

Make the changes in the appSettings section in each of your config and while starting the project, select your build configuration. 在每个配置的appSettings部分进行更改,并在启动项目时选择您的构建配置。

It will take the configuration settings from your appSettings section from that respective config. 它将从相应配置的appSettings部分获取配置设置。

Using an <appSettings> tag in web.config you can supports a file attribute that will load an external config with it's own set of key/values. 使用web.config中的<appSettings>标记,您可以支持一个文件属性,该属性将使用自己的一组键/值加载外部配置。 These will override any settings you have in your web.config or add to them. 这些将覆盖您在web.config中的任何设置或添加到它们。

eg; 例如;

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

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

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

Inside the .config file 在.config文件中

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

Aspx Page Aspx

<a href="<%=ConfigurationManager.AppSettings["webAddress"]%>">L‌​ink</a>

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. 如果由于某种原因,您没有可以指定每个环境变量的自动部署系统(如前面的答案中提到的Octopus),还有其他几个选项。

1: Put your settings in a machine.config file for each environment and deploy that config to the appropriate servers. 1:将您的设置放在每个环境的machine.config文件中,并将该配置部署到适当的服务器。

2: Put your relationships in your config file: 2:将您的关系放在配置文件中:

  <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 由于您没有不同的配置可以切换,为了测试这种方法,您可以将主机文件(c:\\ System32 \\ drivers \\ etc \\ hosts)更改为假冒每个环境

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. 然后注释掉或删除非localhost条目以连接到真实服务器。

From what you can see in the already existing answers, you're going to need to change your static string hrefs for something defined in compile time. 根据您在现有答案中可以看到的内容,您将需要为编译时定义的内容更改静态字符串hrefs I think you should follow the previous advises and write different tags for the Web.Debug.config and the Web.Release.config , but not to solve your specific situation, mostly to define your connection strings in case you have different database sources for each base. 我认为您应该遵循以前的建议并为Web.Debug.configWeb.Release.config编写不同的标签,但不是为了解决您的具体情况,主要是为了定义您的连接字符串,以防每个人有不同的数据库源基础。

To solve your specific situation, just use the Url native MVC helper, and replace your static hrefs for something like, <a href="@Url.Content("~/Contact")"> Contact </a> , where "~/" stands for your domain root, regardless of which one it is. 要解决您的具体情况,只需使用Url原生MVC帮助程序,并将您的静态href替换为<a href="@Url.Content("~/Contact")"> Contact </a> ,其中"~/"代表您的域根目录,无论它是哪一个。

Also, given you didn't write any Controller name before "Contact", I'm assuming you have a ContactController in your application and is reaching its Index View with this address. 另外,假设您在“联系人”之前没有写任何控制器名称,我假设您的应用程序中有一个ContactController,并且正在使用此地址访问其索引视图。

What if you made the Model for the MVC View a Dictionary or added a dictionary of settings to the Model returned and simply returned the data from the controller? 如果您为MVC制作模型查看字典或为返回的模型添加了设置字典并简单地从控制器返回数据,该怎么办?

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

OR 要么

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

Alternatively you could just use a Model that has a ContactUsUrl property 或者,您可以使用具有ContactUsUrl属性的Model

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

Then in your controller code you could use whatever logic you want to decide whether it is DEV, QA or PROD and pull the data for the URL from the associated source, eg a database, a config file, a custom settings file. 然后在您的控制器代码中,您可以使用您想要的任何逻辑来决定它是DEV,QA还是PROD,并从关联的源中提取URL的数据,例如数据库,配置文件,自定义设置文件。 This abstracts the logic and simplifies how your data is used on the page. 这抽象了逻辑并简化了数据在页面上的使用方式。

This is an alternative to using the web.config directly. 这是直接使用web.config的替代方法。

The solutions proposed are good but a lot of overhead just to switch one url IMHO. 提出的解决方案是好的,但只是切换一个网址恕我直言的很多开销。

I would take the environment variable approach and set the url accordingly. 我会采用环境变量方法并相应地设置url。

Contact.cshtml 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 big project and you can invest some time, I would put this in appSettings and set up parameters.xml for each environment and use it during deployment. 如果这是一个大项目,你可以投入一些时间,我会把它放在appSettings并为每个环境设置parameters.xml并在部署期间使用它。

If you want to use DotNet Core or want to switch to it, then it is best to implement it's environment configuration abilities. 如果您想使用DotNet Core或想要切换到它,那么最好实现它的环境配置能力。

Documentation on official site: Working with multiple environments and Configuration in ASP.NET Core 官方站点上的文档: 使用多个环境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 is to define 3 different appsettings files. 您所需要的只是定义3个不同的appsettings文件。 And set ASPNETCORE_ENVIRONMENT in your system there you want to do your tests or developing. 并在您的系统中设置ASPNETCORE_ENVIRONMENT ,您要进行测试或开发。

Or you can use env.IsEnvironment("environmentname") to check something in runtime. 或者您可以使用env.IsEnvironment("environmentname")在运行时检查某些内容。

You don't need to use app settings for this. 您无需为此使用应用程序设置。 Just use the URLHelper that's part of MVC and it'll ensure that the correct hostname is used. 只需使用属于MVC的URLHelper ,它将确保使用正确的主机名。

So instead of this: 所以不是这样的:

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

You do this: 你做这个:

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

That will generate a link that points to the Contact action within the IndexController . 这将生成指向IndexControllerContact操作的链接。

Another option is to use the HTMLHelper extension method to generate the entire tag for you. 另一种选择是使用HTMLHelper扩展方法为您生成整个标记。 In that case, you'd do this: 在这种情况下,你会这样做:

@Html.ActionLink("Index", "Contact", "Contact Us")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM