How to get the base URL of a site in MVC

I want to send an email to the user, where he can click the link to go to my site. I do not want to hardcode the URL in my email templates. I want this dynamic to be in a way, that regardless of the environment, it will send the linked URL. For example, if I am in a development environment, it sends something like http://localhost:port or during the production process it sends the actual URL of the website. http://www.domain.com

I just need to know how to save it in a DynamicViewBag in MVC action. Any suggestion plz?

+6
source share
2 answers

You can do it:

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

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

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

Source: https://habr.com/ru/post/971799/


All Articles