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?
RequestContext); string url = u. Action("About", "Home", null); if you want to create a hyperlink: string link = HtmlHelper.
You add a new action to a controller by adding a new method to the controller. For example, the controller in Listing 1 contains an action named Index() and an action named SayHello(). Both methods are exposed as actions.
Controllers process incoming requests using action methods. Action methods typically have a one-to-one mapping with user interactions. When a user enters a URL into the browser, the MVC application uses routing rules that are defined in the Global. asax file to parse the URL and to determine the path of the controller.
Found it by using Reflector:
string href = Url.Action("DetailsAsXml", new { projectId = item.Id });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With