How to Serve Files from ASP.NET Core Website

Mike-E 171 Reputation points
2020-12-25T16:32:52.117+00:00

I am attempting to setup an ASP.NET Core website and it is basically serving as a proxy/gateway to an Azure Storage blob container. I have requests such as:

localhost/files/<guid>/requested/file.html

And this fetches the file from Azure Storage.

Everything "works" right now, but I am not convinced I have done this the Right™ way. :) What is the best way of doing this from an MVC application? Right now I am doing this from a API controller, returning a FileResult object with the stream from Azure Storage. It seems a little sluggish, so I am wondering if there is further magic/integration with MVC I can utilize to get a faster pull/experience.

This sluggishness could also be from the Storage Explorer account on my machine, but still. It seems like it would be faster considering everything is directly executed and served locally.

Also, since this being served from WebAPI, whenever a non-existant file is encountered, I get the following output:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.4","title":"Not Found","status":404,"traceId":"00-dbe086226eb4dd4c8c0c6e626088eb54-d756f9d25c1b8444-00"}

I would rather get a 404 error page. Since it's API, I am not sure how to route this. I would prefer to pull everything from MVC, but it is unclear if this is even possible.

Thank you for any assistance/suggestions anyone can offer. Happy Holidays out there to all, in any case!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,134 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mike-E 171 Reputation points
    2020-12-31T15:42:41.76+00:00

    Alright, after MUCH further digging, I can confirm the bottleneck is from serving files from the local Azure Storage Blob Emulator running on my local machine.

    The typical time to retrieve and serve the file from Azure Local Emulator is about 120-150ms. Whereas if I serve files directly from disk, the time drops to 15-20ms. Running some profiling the majority of those 15-20ms are spent in EntityFramework pulling the information required to assert authorization to view the file.

    I sure hope this improves when actually deployed to production. I think I read somewhere that the emulator is going away in favor of a new cross platform version.

    In any case, I am satisfied with all my research here now and have the answer. The problem was not ASP.NET and in fact, it was working very well and very intuitively. I can not say enough great things about this product and the team behind it.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Mike-E 171 Reputation points
    2020-12-26T13:14:14.89+00:00

    Just so happened to create a "blank" asp.net core application and pretty much landed on the answer I was looking for:

    D9WyhsC.png

    That's pretty cool. I am pretty sure this is also what happens when a controller is "mapped" as well. I may perhaps be doing things the "right" way after all, but always open to further suggestions.

    Also, I ran my application in Release mode and local Azure Storage seemed a bit better, but still sluggish on the first pull. I guess I will know for certain if this is really an issue after deployment and I am using actual Azure infrastructure.

    As for the 404, I have a custom 404 page that I am utilizing now when the file does not exist. It would be nice to tie into built-in ASP.NET/MVC magic here but after doing some searching around this, it seems more involved than expected/anticipated. So, I guess I am set for now. I was tempted to delete this question but am open to further suggestions/pointers if they are out there.

    0 comments No comments