0

I have a list of URLs that I use in one of my page to render html iframes from them (All URLs are my app actions and each one show a letter to the user). Now I want an action that inside that call each of these actions and merge the result into a single response and return that.

Something like this:

public IActionResult GetAllLettersAsPDF()
{
   List<string> letterUrls = GetLettersList();
   List<string> letterHtmls = new();

   foreach(var letter in letters)
   {
      var letterHtml = // here's what I'm looking for to turn the url into string responce

      letterHtmls.Add(letterHtml);
   }

   return MergeIntoSinglePDF(letterHtmls);
}

My question is whether there is a way in AspCore that I can call an action by having it's URL and get the string response from that.

My bottom-line solution is to use an HttpClient and call '127.0.0.1' from that for each action to gain the response but I'm wondering if there is more clean and efficient way to do that in ASP that does not involve networking and etc.

HamedH
  • 2,814
  • 1
  • 26
  • 37
  • Does this answer your question? [ASP.Net Core Call a controller from another controller](https://stackoverflow.com/questions/34963586/asp-net-core-call-a-controller-from-another-controller) – Christoph Lütjen Sep 30 '21 at 11:14
  • No, in fact I can do some reflection to call the desired method, in addition to it's not ideal, it will gave me IActionResult and I have no idea how the turn the IActionResult into string. – HamedH Sep 30 '21 at 11:24
  • " IActionResult into string" you could post the action. That would allow others to help you. Btw. a "clean" way would be to move the functionality into services and then you would use these services in your GetAllLettersAsPDF() action instead of calling/fetching other actions. – Christoph Lütjen Sep 30 '21 at 11:38
  • **DON'T MIX** the backend with the frontend. instead, refactor your actual business logic on these letters (at the business layer and not at the controllers layers). – iSR5 Sep 30 '21 at 11:50
  • @iSR5, I don't think it's Mixing, I have some letters that are mostly cshtml files, and a few lines of cs in each action in the controller that feed the needed data to the razor file. that's what a controller is supposed to do, feeding data to view to generating html results. Now I want the same html result so it's Ok if a controller is involved, but the required code to employ an asp controller is missing and that's what I'm looking for – HamedH Sep 30 '21 at 12:20
  • @HamedH then your best bet is to use the razor instead of the controllers, do your work at the fontend. (use JS to call endpoints, reterive the files, work with `PDF` stuff, download them. you could do that with the help of jquery and `pdflibjs` with ease. just adjust your controller to provide the proper endpoints, the rest should be handled at the front-end. – iSR5 Sep 30 '21 at 12:25

0 Answers0