3

I intend to return a static html page from my controller action. The html page is part of my project / bundle. I have the following code which works when run locally (localhost), but when on Azure, it fails. I don't have access to the Azure instance at the moment so I can not debug it or turn on Development mode for detailed errors etc. Please excuse the naiveness, what is the issue with the following code:

    public IActionResult PrivacyPolicy()
    {
        //_hostingEnvironment is an instance of IHostEnvironment
        var staticPage = PhysicalFile(System.IO.Path.Combine(_hostingEnvironment.ContentRootPath, "Views/Home/PrivacyPolicy.html"), "text/html");
        return staticPage;
    }
7
  • 1
    Try: return File("Views/Home/PrivacyPolicy.html", "text/html"); Commented Dec 16, 2019 at 23:37
  • @RobertMcKee with that I get FileNotFoundException Commented Dec 16, 2019 at 23:43
  • Are you deploying the file in that location? Not under content? Or with File, try AppRelative like "~/View/Home/PrivacyPolicy.html" Commented Dec 16, 2019 at 23:51
  • 1
    File("~/Views/Home/PrivacyPolicy.html", "text/html") file not found exception when running locally, I'm now seeking access to Azure to see where that file is going, it might not be getting published, wouldn't know until then Commented Dec 16, 2019 at 23:55
  • @pnizzle also as Darin says, you may want to put your static file in a different directory than Views Commented Dec 17, 2019 at 0:04

2 Answers 2

2

If you have access problems you can simply read the file and then just return its content:

return Content(File.ReadAllText("somefile.html"));

Darin Dimitrov also answered a question about this, that uses a different approach. please also have a look at that:

How do you request static .html files under the ~/Views folder in ASP.NET MVC?

Also as Darin says, you may want to put your static file in a different directory than Views

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

2 Comments

I just took Darin's advice there on not being able to access the Views directory. I placed the files elsewhere and all is well now, both locally and on Azure. Thats using _webHostEnvironment.WebRootPath. Other approaches may work too. I have not verified.
@pnizzle Great.
0

Depending on what sort of host OS you run your application you might want to check whether your "Views/Home/PrivacyPolicy.html" is spelled with correct case letters. Reason being, some filesystems (like ext4 on Ubuntu) are case sensitive.

1 Comment

Its hosted on a Windows instance, in Azure. But locally I am running it on a Mac, through Visual Studio for Mac. I've checked the casing, all is correct.

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.