How to call RouteUrl method from C# class

Sheonarayan
Posted by Sheonarayan under ASP.NET MVC category on | Points: 40 | Views : 2228
I had a requirement where I needed to call RouteUrl method from C# code that would generate a URL based on the controller, action and other parameter passed. In general, its find to proper object to call this from.

Here is the solution of that problem

var url = new UrlHelper(HttpContext.Current.Request.RequestContext).RouteUrl("Default", 
new { controller = "Drawings", action = "Details", id = item.AutoId, title = item.Title }));

We need to instantiate UrlHelper class that exists in System.Web.Mvc namespace and pass necessary parameter.

Hope this helps.

Comments or Responses

Login to post response