I have an endpoint that should return a value from an excel file.
On localhost this works by enabling UseStaticFiles
middleware using this code, and adding excel file into the Resources
folder:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "Resources")),
RequestPath = "/Resources"
});
However when I publish it in the Azure hosting it shows this error and I cannot acces my application nor this excel file.
After I remove the UseStaticFiles
middleware, application works well except for the missing excel file.
So how should I publish and access this file in the Azure environment?
2
Answers
I combined Clean Windows Azure Website and Accessing wwwroot folder to come up with the solution.
No middleware is required, just by using
IWebHostEnvironment
service I could access needed excel file, which is now based in thewwwroot/Resources/
directory within my project.The
UseStaticFiles
method only, and I quote, "Enables static file serving". This does not enable you to return a value from a specific cell in an Excel file.If you’re looking for serving static files you could for instance have a look at putting the file in Azure Storage and using the desired public access level.
If you’re looking to read from an Excel file, you might want to have a look at a way to do so that does not require Interop since you will not have Excel available on an App Service.
On option would be to use the OpenXML XML SDK.
Another would be an open source solution like ClosedXML which is a wrapper around OpenXML making working with them easier.