0

Working with ASP.Net Core is still a little new to me. I'm trying to follow a tutorial on PluralSight that details how Image Composition in .NET works.

That said, the code I'm trying to convert from ASP.Net MVC to ASP.Net Core is showing an error on Request in my Index.cshtml:

<img src="@Url.Action("ImageFromPath", new { path = Request.MapPath("~/img/1.jpg") })" />

The error is simply:

The name 'Request' does not exist in the current context.

My HomeController.cs:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public ActionResult ImageFromPath(string path)
    {
        var ms = new MemoryStream();
        using (Bitmap bitmap = new Bitmap(path))
        {
            bitmap.Save(ms, ImageFormat.Jpeg);
        }

        ms.Position = 0;
        return File(ms, "image/jpg");
    }
}

In MVC, I can look up definition of Request that goes all the way back to WebPageRenderingBase [from metadata], yet this does not exist in .NET Core, can anyone offer a way to get this to work?

EDIT - I later plan on using the backend ImageFromPath method to return an image from an API call, not the file system.

LatentDenis
  • 2,839
  • 12
  • 48
  • 99

2 Answers2

2

You could just serve the image straight from file system, unless I'm missing something.

<img src="@Url.Content("~/img/1.jpg")" alt="Some Text" />

Update

I later plan on using the backend ImageFromPath method to return an image from an API call, not the file system.

You just pass the image filename only, and resolve the full path at server-side.

<img src="@Url.Action("ImageFromPath", new { filename = "1.jpg" })" />

public class HomeController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public HomeController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public ActionResult ImageFromPath(string filename)
    {
        string path = _hostingEnvironment.WebRootPath + "/img/" + filename;
        ...
    }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • Trying your solution - I get an error: `A null reference or invalid value was found [GDI+ status: InvalidParameter]` Any idea why this is the case in Debug/Development-mode? (Could be a different case if publishing to an app service) – LatentDenis Jul 06 '17 at 15:46
  • Could you try like [this](https://stackoverflow.com/a/35324326/296861)? – Win Jul 06 '17 at 15:58
  • Does that go into the `ConfigureServices` function or in the actual `StartUp`? – LatentDenis Jul 06 '17 at 16:04
  • I never have IHostingEnvironment null, so I did not get change to test it. You can try both and see what works. – Win Jul 06 '17 at 16:29
0

You can inject IHostingEnvironment variable in view and rewrite code like below using Host variable:

 @using Microsoft.AspNetCore.Hosting
 @inject IHostingEnvironment Host;

 <img src="@Url.Action("ImageFromPath", new { path = Host.WebRootPath + "/img/1.jpg" })"

UPDATE:

Did not see Win's update message, his solution use the same approach, but more cleaner in terms of separation of concerns than my solution.

user2771704
  • 5,994
  • 6
  • 37
  • 38
  • 1
    Please do not do that. Malicious user can *easily* find out the directory structure of the web server, and it'll lead to a security hole. – Win Jul 06 '17 at 15:33