Search code examples
c#asp.net-corerazor

How to load and display a .html file from an external source into a razor page


I have searched this forum, but similar questions seem to deal with loading an html file from within the server.

I have some files on an external source (Azure BLOB as it happens) with public urls like: https://blah.blob.core.windows.net/public/Legal/Privacy.html (not the real one)

I want to load them into my razor page and display the contents.

I am passing the URL of the file into the page using the querystring:

    public class ShowPageModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public string urlPath { get; set; }

This works in terms of getting it to the code behind.. I tried using an iFrame:

<iframe src=@Model.urlPath></iframe>

This just loads an empty frame then downloads the file..

I also tried using partial:

<partial name="@Model.urlPath" />

But this is for on server use I think.. it had a not found exception

I imagine it would be a File.read type of thing but that only support on server it seems...

I do it on the Blazor side of this project using MarkupString but Im not sure how to set this up using the server side razor page... Any ideas? Thanks


Solution

  • You could try fetch to raw htmlstring and use @html.raw() to render.
    Index.cshtml.cs

        public class IndexModel : PageModel
        {
            public string htmlstring;
            public async Task OnGet()
            {
                HttpClient client = new();
                htmlstring = await client.GetStringAsync("http://localhost:5080/index.html");
            }
        }
    

    Index.cshtml

    @page
    @model IndexModel
    
    @Html.Raw(@Model.htmlstring)
    

    Test
    enter image description here