如何在MVC中获取网站的基本URL [英] How to get base url of the site in MVC
问题描述
我想向用户发送电子邮件,他可以单击链接以转移到我的网站.我不想在我的电子邮件模板中对URL进行硬编码.从某种意义上说,我希望这种动态变化,无论环境如何,它都会发送相关的网址.就像如果我在开发环境中,它发送类似http://localhost:port
的东西,或者在生产中发送实际的网站URL. http://www.domain.com
I want to send an email to the user where he can click on link to transfer to my site. I don't want to hard code the url in my Email Templates. I want this dynamic in a sense that whatever the environment it will send the related url. Like If I am on the development environment it send something like http://localhost:port
or in production send the actual website url. http://www.domain.com
我只需要知道如何将其保存在MVC Action的DynamicViewBag
中.有什么建议吗?
I just need to know how can I save it in DynamicViewBag
in MVC Action. Any suggestion plz?
推荐答案
您可以执行以下操作:
var dynamicViewBag = new DynamicViewBag();
dynamicViewBag.AddValue("BaseUrl", Request.Url.GetLeftPart(UriPartial.Authority));
这篇关于如何在MVC中获取网站的基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!