1

I'm migrating my WebAPI to .Net Core, the project contains razor files (.cshtml) which I'll compile at runtime when a method is called and return an HTML file.

Before migrating, all files were accessible to use, after that, I can only access the files under "wwwroot", excluding file types like .cshtml which are always hidden when deployed on Azure.

It's working perfectly on my machine, the issue is when it's deployed live. I've tried adding and removing app.UseStaticFiles().

I'm using Razorlight to access the razor templates, it's throwing an error saying that the file doesn't exist.

2
  • Are the cshtml files included when you deploy to azure? If you are using Azure WebApp you can navigate to <your-web-app-name>.scm.azurewebsites.net and browse the file system of your web app. Can you confirm that the files have been deployed? Commented May 28, 2019 at 9:25
  • I've checked the availability of the files via FTP, it seems that the .cshtml files aren't being uploaded, only css, js, and images files are showing under the wwwroot. Commented Jun 5, 2019 at 7:37

1 Answer 1

4

app.UseStaticFiles() only serves the web root(wwwroot) directory. If you need to serve static files from outside the web root directory then you need to set up the StaticFileOptions as shown below.

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles(); // For the wwwroot folder

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
        RequestPath = "/StaticFiles"
    });
}

You can then serve files from MyStaticFiles directory like:

http://<server_address>/StaticFiles/MyViews/test.png serves the test.png file

See - Static Files

As mentioned on the comments, ensure that 'MyStaticFiles'(different in your case) exists by navigating to <your-web-app-name>.scm.azurewebsites.net and checking you project hierarchy.

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

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.