In an asp.net-mvc site, is there a better way to get the full base URL?
I have an asp.net-mvc site and I send out a lot of email from my site.
Before I used to send email from my controller and I used this code to get
the base url:
protected string GetBaseUrl()
{
return string.Format("{0}://{1}{2}", Request.Url.Scheme,
Request.Url.Authority, Url.Content("~"));
}
that is because i obviously need fully formed URL. so in a normal link on
a page that i have href="/GoHere", i would want that to translate to:
"http://www.mysite.com/GoHere"
not just relative URLs given that they are going in emails like
"/GoHere"
I am now refactoring my controller to move all of this code outside of it,
but i find myself passing this baseURL string around (because the function
above relies on Request which is in namespace:
System.Web
and I can't seem to access this request object outside the controller
class. Right now I am passing a string BaseURL all over the place so when
i need to generate the emails, i can append the relative URL after the
base URL but that feels very hacky
Is there a better way to get the baseURL (either through the Request
object or not) outside of a controller class in an asp.net-mvc website?
No comments:
Post a Comment