Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an action link in a Controller

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?

like image 574
Roger Lipscombe Avatar asked Apr 29 '09 09:04

Roger Lipscombe


People also ask

How do you create a link on a controller?

RequestContext); string url = u. Action("About", "Home", null); if you want to create a hyperlink: string link = HtmlHelper.

How do I add actions to my controller?

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.

What is action method in controller?

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.


1 Answers

Found it by using Reflector:

string href = Url.Action("DetailsAsXml", new { projectId = item.Id }); 
like image 143
Roger Lipscombe Avatar answered Oct 08 '22 01:10

Roger Lipscombe