Monday, April 23, 2012
ASP.NET MVC 3 Generating an ActionLink in the controller - Html.ActionLink Equivalent
Generating routing links is extremely easy in MVC 3 using @Html.ActionLink(linkText,action,controller) when you're using Razor views this is great. However sometimes you need to generate the link within the controller - at which point you do not have direct access to the Html helper.In these instances such as creating links for a wysiwyg editor or adding a link into an email it is preferable to have the system generate it with the appropriate protocol http/https and/or a port number if you're running locally. To this end I did some digging and put together the following little code snippet which does eactly that!
I thought I'd share it here to save me having to look it up again in the future!
string absoluteAction = string.Format("{0}://{1}{2}",
Url.RequestContext.HttpContext.Request.Url.Scheme,
Url.RequestContext.HttpContext.Request.Url.Authority,
Url.Action("ActionName", "ControllerName", new {Id = "Data"}));
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment