Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting route to controller action (url) programmatically

I have a controller named "AccountController" and action called "ResetPassword". From the html markup I can do something like:

<%= Html.ActionLink("LinkText", "ResetPassword", "AccountController") %>

and it will output the correct url. Is there a way to do this programmatically? I am trying to get the url as a string in another class but System.Web.Mvc.HtmlHelper doesn't have an ActionLink() static method and creating an instance of the class doesn't have it either.

Note: If I try to create instances of HtmlHelper or UrlHelper I then need references to the View Context or Request Context and I can't figure out how to get those from the controller (to pass in to my class method)

like image 385
codette Avatar asked Apr 18 '09 22:04

codette


People also ask

How do you give a controller a URL?

UrlHelper u = new UrlHelper(this. ControllerContext. RequestContext); string url = u. Action("About", "Home", null);

What is URL routing in MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller.

What is redirect to route in MVC?

RedirectToRoute(String, Object)Redirects to the specified route using the route name and route values.

What is a URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.


1 Answers

Use Url.Action instead.

like image 161
Daniel A. White Avatar answered Sep 18 '22 02:09

Daniel A. White