Chris
Chris

Reputation: 27384

Serve up file from outside wwwroot

I am migrating an ASP.NET4.5 website to ASP.NET 5. One function we had returned images off the hard disk from an absolute location. The files arent stored within the web directory. Previously this worked fine, with the following code:

public ActionResult GetVideoImage(string serialNumber, int videoEntryId)
{
    try
    {
        var serial = Device.FriendlySerialNumberToNumericalSerialNumber(serialNumber);

        var entry = this.service.GetVideoEntry(serial, videoEntryId);

        if (entry != null && System.IO.File.Exists(entry.FirstVideoFrameLocation.LocalPath))
        {
            return this.File(entry.FirstVideoFrameLocation.LocalPath, "image/jpeg"); // adjust content type appropriately
        }
    }

    return this.Redirect("/content/noimage.png");
}

Unfortunately this doesnt work anymore and throws an exception. From what I can tell its because this.File now takes a virtualPath rather than an absolute one so balks at the idea of serving a file from outside of its web directory.

Upvotes: 0

Views: 1601

Answers (1)

Chris
Chris

Reputation: 27384

I found the answer on a MS thread here that links to a ASP github commit.

Long story short there are new classes available in the Microsoft.AspNet.Mvc namespace that allow the thing I'm looking for. I specifically chose PhysicalFileResult which works as expected

Upvotes: 2

Related Questions