0

I am really struggling from the below problem for long time. That would really helpful and appreciated if anyone could solve the problem!!

Environment:

  • AspNetCore3.1
  • MacOS

Problem:

  • Cannot invoke a controller method from a cshtml file using the Url.Action method when generating a PDF from the cshtml file with the Rotativa library.

Sample Code

  • Test.cshtml

@{
    Layout = null;
    var imageName = "sampleImage.png";
    var result = Url.Action("GetImage", new { imageName });
}

<div>
  <img src="@result" alt="error" />
</div>
  • TestController.cs

namespace Test.Controllers
{
    public class TestController : Controller
    {
        private readonly IWebHostEnvironment _env;

        public TestController(IWebHostEnvironment env)
        {
            this._env  = env;
        }
    
        [HttpGet]
        [Route("[action]")]
        public IActionResult GetImage(string imageName)
        {
            string path = Path.Combine(this._env.WebRootPath,"images",imageName);
            byte[] bytes = System.IO.File.ReadAllBytes(path);
            return File(bytes, "image/png");
        }
    }
}
  • It has been a long time since I used Mvc, but shouldn't you be using `Html.RenderAction` or something? I thought that `Url` was used as a helper for generating url's and such. I could be wrong. :) – Silvermind May 28 '21 at 13:46
  • Thank you for your reply. Is the method "Html.RenderAction" available in AspNetCore 3.1? According to the official doc, it applies to AspNet MVC 5.2. – Takumasakix May 28 '21 at 13:53
  • One or more of the answers to this question might help you: https://stackoverflow.com/questions/41353874/equivalent-of-html-renderaction-in-asp-net-core – Silvermind May 28 '21 at 14:19

0 Answers0