Search code examples
c#asp.net-mvccontrollerurlhelper

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


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.


Solution

  • 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);