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