How to get a URL using routing tables from a Service Class?

MongooseNX

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"});

alisabzevari

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.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 2: how to get params from URL without using Routing?

How to get url parameter in activated link in angular 7 and call rest api from service class

How to get parameter from URL(Routing) Angular 2?

how to get original url from routing in asp.net?

Impossible to get JSON from Service into angularJs Routing

Custom dynamic routing and get ids from url

How to get a redirection url that appears temporarily before routing to another url using Selenium?

How to get a variable from a Class using pygame

How to get href from class using python?

How to get class function from a service that return a model

How to get the anchor from the URL using jQuery?

How To Get Input From URL using javascript

How to get bitmap from URL using Coil?

how to get id from URL using php

How to get a param from URL Rest Service that is after a question mark (?)

How to get columns from two tables using join in hibernate

how to get values from nested tables using beautifulsoup

How to get multiple tables using JSoup from a website

How to get data from additional tables using EF/LINQ

How to get rows of Data from Multiple Tables using LinQ To Entities

How to get data from other tables using Sequel

How can I get data from joined tables using fetchxml

how to get data from multiple tables in sql using php

how to get data from two tables Using JOIN

How to use get value from multiple tables using left join?

How do I get all the tables from a website using pandas

Get data from 2 different tables using JPA jax-rs REST service

How to configure google load balancer to route to cloud run service using url mask but strip service from url

How to ignore sitemap url from Laravel routing?