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.

Dev-Site:
https://localhost:43444/Page1

Test-Site:
https://www.domain.com/Preview/Page1

Environment:
ASP.net Core 3.1 MVC

It's clear that the image on the test site referes to https://www.domain.com/Images/Page1.png.
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:

wwwroot/pages/Page1.htm:
<img src='/Images/Page1.png' />
<img src='~/Images/Page1.png' />


Pages/Page1.cshtml:
@{
    var htmlContent = System.IO.File.ReadAllText(@$"wwwroot\pages\Page1.htm");
}

@Html.Raw(htmlContent)


Microsoft's approach:
// Source: https://docs.microsoft.com/de-de/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1
app.UseStaticFiles(new StaticFileOptions {
      FileProvider = new PhysicalFileProvider(
          Path.Combine(env.ContentRootPath, env.ContentRootPath)),
          RequestPath = "~/"
  });
Posted
Updated 5-Oct-20 4:12am
v2
Comments
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 -> Domain.com-Website (ASP.net 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.
   
v3

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