in asp.net-mvc controller, what is the best way to generate a URL
right now i take the
RequestContext
and p开发者_运维问答ass 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.
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);
see ASP.NET MVC create absolute url from c# code
精彩评论