Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an html page from razor page

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

like image 403
Emmanuel_InfTech Avatar asked Sep 03 '25 02:09

Emmanuel_InfTech


1 Answers

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

like image 95
Rena Avatar answered Sep 04 '25 15:09

Rena