Click here to Skip to main content
14,774,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
A static HTML file is displayed (as raw) in a simple view.
Problem is that urls in the html are not mapped "correctly" (in my case), because the test site is hosted in a sub folder.



Environment: Core 3.1 MVC

It's clear that the image on the test site referes to
And (of course) changing the src to "~/Images/Page1.png" is not evaluated.

There is a solution from Microsoft but which path do I have to set here?

What I have tried:

<img src='/Images/Page1.png' />
<img src='~/Images/Page1.png' />

    var htmlContent = System.IO.File.ReadAllText(@$"wwwroot\pages\Page1.htm");


Microsoft's approach:
// Source:
app.UseStaticFiles(new StaticFileOptions {
      FileProvider = new PhysicalFileProvider(
          Path.Combine(env.ContentRootPath, env.ContentRootPath)),
          RequestPath = "~/"
Updated 5-Oct-20 4:12am
F-ES Sitecore 5-Oct-20 7:03am
If it's literally just a sub-folder then your site probably won't run at all, it will need to be inside a virtual directory. You can use @Url.Content in your views to reference static files, but as I said none of these techniques will work if your site is simply in a normal sub-folder.

1 solution

It's an application within a website in IIS
IIS -> ( 4.5) -> Add Application-> Preview

So the IIS configuration should be good. I just have to replace the (/Images)-Url in the HTML files with the final path. As I load the HTMLs using System.IO and treat it as plain text the net Core Host does not change the path by itself. That's the point there the Microsoft approach should take place.

The easiest way would be to simple replace ~/ via code by ~/Preview/, but I'm not sure if this is just a quick'n dirty way.
htmlContent = htmlContent.Replace("~/","/Preview/") //or using regex;

[2020-10-07] I've solved this in the described way, which works good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900