Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render static html file in MVC Core?

I'm using static files middleware to access customer specific resources located outside wwwroot. These resources include CSS and image files, and are accessible at ~/resources. For example, customer-specific css is located here :

 <link rel="stylesheet" href="~/resources/css/customer.css" />

Now I would like to serve a static html file located in ~/resources/html/static.html. I created a model which contains only a HtmlString property, and I wish to inject the content of static.html inside a standard cshtml using this model. Here is my controller action :

public IActionResult MentionsLegales()
{
    var filePath = IO.Path.Combine(
        _env.ContentRootPath, "PersonnalisationClient", _config["PersonnalisationClient:NomClient"], "html", "mentionslegales.html");

    var mentionsLegalesModel = new MentionsLegalesModel();

    if (IO.File.Exists(filePath))
        mentionsLegalesModel.Content = new Microsoft.AspNetCore.Html.HtmlString(IO.File.ReadAllText(filePath));
    else
    {
        _logger.LogWarning($"Le fichier {filePath} est introuvable");
        mentionsLegalesModel.Content = new Microsoft.AspNetCore.Html.HtmlString(string.Empty);
    }
    return View(mentionsLegalesModel);
}

As you can see, here I access the file through its physical path. It's working but I'm not satisfied with this solution. I wanted to read the file through its server path, which is ~/resources/html/mentionslegales.html, but I don't know how to do that. Apparently in non-Core MVC one could use Server.MapPath for such purpose, but I couldn't find an equivalent in MVC core. Do you have any ideas ?

like image 272
BlackMushroom Avatar asked Sep 19 '25 22:09

BlackMushroom


1 Answers

If you want to call the static resources in the folder outside the web root directory in the page, you could configure the Static File Middleware in Configure method as follows:

app.UseStaticFiles(); // For the wwwroot folder

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "resources")),
    RequestPath = "/resources"
});

Then reference the file like below:

 <link rel="stylesheet" href="~/resources/css/customer.css" />

return View() is reserved for returning the result of a view execution, i.e. a Razor CSHTML page. You can't use an HTML page for that.Try to redirect directly:

return Redirect("~/resources/html/mentionslegales.html");

Refer to the links below which may be help you better understand how to return static html pages from the controller.

How to make a get request for an HTML file using ASP.NET Core MVC

https://github.com/aspnet/Mvc/issues/3751

Update:

For injecting the static html in the View , you could refer to :

Controller

 public IActionResult GetStaticFile()
 {
        var mentionsLegalesModel = new MentionsLegalesModel();
        var htmlString = System.IO.File.ReadAllLines("./resources/html/mentionslegales.html");
        mentionsLegalesModel.Content = string.Join("", htmlString);
        return View(mentionsLegalesModel);
  }

View:

@model MentionsLegalesModel

@Html.Raw(Model.Content)

Also you could try the Partial view suggested on the above first link

like image 124
Xueli Chen Avatar answered Sep 21 '25 10:09

Xueli Chen