views:

140

answers:

3

In one of my controller actions, I'm generating some XML. One of the attributes in that XML is an href to another controller and action, with some parameters. The XML should look something like this:

<projects>
  <project id="42" name="Project X", href="/projects/42"/>
  <!-- etc. -->
</projects>

I don't mind if the URL is relative or absolute, but my question is this: how do I generate the URL in the controller code, in a type-safe way?

In other words, how do I do what HtmlHelper.ActionLink does, but from a controller?

A: 

The link isn't entirely what you're after, but I think you should be able to use a similar mechanism - pass route data (available from the controller's RouteData property) to RouteTable.Routes.GetVirtualPath

RedirectToAction in ActionFilterAttribute

chrisb
+2  A: 

Found it by using Reflector:

string href = Url.Action("DetailsAsXml", new { projectId = item.Id });
Roger Lipscombe
A: 

Please using URLHelper

Soul_Master