asp.net-mvc 如何在MVC中获取网站的基本网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24585464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:46:05  来源:igfitidea点击:

How to get base url of the site in MVC

asp.net-mvcasp.net-mvc-3asp.net-mvc-4razor

提问by Ammar Khan

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:portor in production send the actual website url. http://www.domain.com

我想给用户发送一封电子邮件,让他可以点击链接转移到我的网站。我不想在我的电子邮件模板中对 url 进行硬编码。我想要这种动态,无论环境如何,它都会发送相关的 url。就像如果我在开发环境中,它会发送类似http://localhost:port或在生产中发送实际网站 url 的内容。http://www.domain.com

I just need to know how can I save it in DynamicViewBagin MVC Action. Any suggestion plz?

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

回答by

You can use the properties of the Request object, for example

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

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

回答by David

You can do this:

你可以这样做:

var dynamicViewBag = new DynamicViewBag();
dynamicViewBag.AddValue("BaseUrl", Request.Url.GetLeftPart(UriPartial.Authority));