如何在MVC中获取站点的基本URL

Amm*_*han 8 asp.net-mvc razor asp.net-mvc-3 asp.net-mvc-4

我想向用户发送一封电子邮件,他可以点击链接转移到我的网站.我不想在我的电子邮件模板中对网址进行硬编码.我希望这种动态在某种意义上说它会发送相关网址的环境.喜欢如果我在开发环境中它发送类似http://localhost:port或在生产中发送实际的网站URL.http://www.domain.com

我只需要知道如何DynamicViewBag在MVC Action中保存它.有什么建议吗?

小智 16

例如,您可以使用Request对象的属性

var request = HttpContext.Current.Request
var address = string.Format("{0}://{1}", request.Url.Scheme, request.Url.Authority);
Run Code Online (Sandbox Code Playgroud)


Dav*_*vid 10

你可以这样做:

var dynamicViewBag = new DynamicViewBag();
dynamicViewBag.AddValue("BaseUrl", Request.Url.GetLeftPart(UriPartial.Authority));
Run Code Online (Sandbox Code Playgroud)