0

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. enter image description here

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?

1
  • Read this. learn.microsoft.com/en-us/aspnet/core/fundamentals/… . You have either a permission problem if the server is Linux or a "CopyAlways" missing if it is windows. But read the documentation step by step and for sure you will figure it out. Commented Dec 28, 2021 at 13:11

2 Answers 2

1

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.

The SDK is built on the System.IO.Packaging API and provides strongly-typed classes to manipulate documents that adhere to the Office Open XML File Formats specification.

[...]

The Open XML file formats are useful for developers because they are an open standard and are based on well-known technologies: ZIP and XML.

Another would be an open source solution like ClosedXML which is a wrapper around OpenXML making working with them easier.

ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem was accessing excel file when application is hosted in Azure. I will check out Azure Storage, thank you!
0

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 the wwwroot/Resources/ directory within my project.

private readonly IWebHostEnvironment _env;

public ProjectService(IWebHostEnvironment env)
{
   _env = env;
}

public double GetExcelValue() 
{
   string excelFilePath = Path.Combine(_env.WebRootPath, "Resources/file.xlsx");

   (...)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.