I'm writing a asp.net MVC 5 app, and have a service class that gets called from the controller. In the service I'm loading a DTO based on some queried data and would like to build a URL using routing similar to how I do in my Razor code. @Url.Action("RequestData", "Home", new {area = "MyArea"})
How can I get MVC to build my URL using the routing tables from a class without access to the base controller class?
Example:
public class MyDataService : IMyDataService
{
private readonly MyDataContext _context;
public MyDataService()
: this(new MyDataContext())
{
}
public MyDataService(MyDataContext ctx)
{
if (ctx != null)
_context = ctx;
}
public List<MyDTO> LoadDTOInfo(int id)
{
var people = _context.People.ToList();
var list = new List<MyDTO)();
foreach(var person in people)
{
var dto = new MyDto{
MyUrlLink = ...??? // controller/action/+person.Id;
};
list.add(dto);
}
return list;
}
}
using @alisabzevari ANSWER:
UrlHelper helper = new UrlHelper();
helper.Action("ActionName","Controller", new{area = "area"});
You can instantiate a UrlHelper
class. Url
at razor view and controller are both an instance of UrlHelper
class. This class have Action
method and you can use it to generate urls based on routing.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments