HttpContext.Request.Host.Value 适用于多个环境

huangapple go评论73阅读模式
英文:

HttpContext.Request.Host.Value for multiple environments

问题

I have multiple environments:

Local
Test
UAT
Production-Pre
Production-Live

All environments are used for different purposes.

When an environment is created, normally we would amend the web.config file to amend the domain name to use for each environment.

The code normally is used as below

var dom = ConfigurationManager.ApplicationSettings["domain"];
string doSomething = string.format("{0}/sendpayment/{1}", dom, 100);
string doSomethingElse = string.format("{0}/sendemail/" dom);

So when a new environment is created, the config file domain value is changed accordingly to match the domain that has been created/setup in IIS.

I recently saw someone using this in their code

HttpContext.Request.Host.Value

So

string doSomething = string.format("{0}/sendpayment/{1}", HttpContext.Request.Host.Value, 100);

which I thought is better as no more config is required in the web.config.

Recently the environment that was using this code failed as the site was introduced with https. Initial look at it seems the protocol after going to https was missing i.e. it was http and no redirects in place meant it was failing to use https.

The sites with the value in the config file were all fine as a manual change was made and were backup and running but using HttpContext.Request.Host.Value caused this minor issue.

Is there a better way to retrieve the domain name including the protocol or better that I stick with the old traditional way and leave the setting in the config file?

英文:

I have multiple environments:

Local
Test
UAT
Production-Pre
Production-Live

All environments are used for different purposes.

When an environment is created, normally we would amend the web.config file to amend the domain name to use for each environment.

The code normally is used as below

var dom = ConfigurationManager.ApplicationSettings["domain"];
string doSomething = string.format("{0}/sendpayment/{1}", dom, 100);
string doSomethingElse = string.format("{0}/sendemail/" dom);

So when a new environment is created the config file domain value is changed accordingly to match the domain that has been created/setup in IIS.

I recently saw someone using this in their code

HttpContext.Request.Host.Value

So

string doSomething = string.format("{0}/sendpayment/{1}", HttpContext.Request.Host.Value, 100);

which i thought is better as no more config is required in the web.config.

Recently the environment that was using this code failed as the site was introduced with https. Initial look at it seems the protocol after going to https was missing i.e. it was http and no redirects in place meant it was failing to use https

The sites with the value in the config file were all fine as a manual change was made and were backup and running but using HttpContext.Request.Host.Value caused this minor issue.

Is there a better way to retrieve the domain name including the protocol or better that i stick with the old traditional way and leave the setting in the config file?

答案1

得分: 1

Concatenate HttpContext.Request.Scheme with HttpContext.Request.Host.Value.

var domain = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.Value}";
英文:

Concatenate HttpContext.Request.Scheme with HttpContext.Request.Host.Value.

var domain = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.Value}";

huangapple
  • 本文由 发表于 2023年4月17日 21:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定