Takumasakix
Takumasakix

Reputation: 33

Cannot invoke a controller action in a cshtml file used for generating a PDF, using Rotativa Library

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

Environment:

Problem:

Sample Code

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

<div>
  <img src="@result" alt="error" />
</div>

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

Upvotes: 0

Views: 107

Answers (0)

Related Questions