in asp.net-mvc controller, what is the best way to generate a URL

200 Views Asked by At

right now i take the

 RequestContext 

and pass this into a UrlHelper like this:

UrlHelper u = new UrlHelper(context);
string hrSyncUrl = u.Action("Update", "Person");

but the issue is that this seems to return:

/Person/Update

instead of:

http://www.mysite.com/Person/Update

so, given a controller and and action name, how can i generate a FULL url from inside a controller?

the reason that i need this is that i am generating an email so i need the full url to put in the body of that email.

2

There are 2 best solutions below

0
On BEST ANSWER

By using the proper overload:

string hrSyncUrl = u.Action("Update", "Person", null, "http");

And to avoid hardcoding the protocol you could fetch it from the request:

var protocol = context.HttpContext.Request.Url.Scheme;
string hrSyncUrl = u.Action("Update", "Person", null, protocol);
0
On