英文:
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}";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论