Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net Core Controller action, return static html page

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;
    }
like image 284
pnizzle Avatar asked May 19 '26 15:05

pnizzle


1 Answers

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

like image 126
Ashkan Mobayen Khiabani Avatar answered May 22 '26 05:05

Ashkan Mobayen Khiabani