3

I have this ASP.NET Core MVC project:

enter image description here

I want to open the HTML element (html page) named Impresorashtml.html from Index.cshtml using

<a href="/Impresorashtml.html" target="_blank"> Impresoras </a>

The page opens, but I get an error:

This localhost page can't be found

enter image description here

1 Answer 1

2

It is a Razor Pages Project. Any way, no matter Razor Pages or MVC project, the static file should be located in the wwwroot folder by default. Then you could easily access it.

  1. If you place the Impresorashtml.html in the root of the wwwroot folder. You can access it by using code below:

    <a href="/Impresorashtml.html" target="_blank"> Impresoras </a>
    

    enter image description here

  2. If you place the Impresorashtml.html in the sub folder of the wwwroot folder. You can access it by using code like below:

    <a href="/html/Impresorashtml.html" target="_blank"> Impresoras </a>
    

    enter image description here

If you want to serve files outside of wwwroot like what you did(place it in the Pages folder), you need configure the static file middleware as follows:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Pages")),
    RequestPath = "/StaticFiles"
});

Then access it by using the code below:

<a href="/StaticFiles/Impresorashtml.html" target="_blank"> Impresorashtml</a>

Reference:

Serve files outside of web root

2
  • Thanks. Should I move the html element (the page file) using the solution explorer in Visual Studio? Or should I move using the Windows folders (Windows explorer)? Commented Mar 3, 2023 at 6:18
  • Hi @Emmanuel_InfTech, pls check my answer carefully, I also share another solution if you do not want to move the html file to the wwwroot, just add the middleware and change your href of anchor link.
    – Rena
    Commented Mar 3, 2023 at 6:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.