如果要调用静态资源Web 根目录之外的文件夹 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-files-outside-of-web-root在页面中,您可以配置静态文件中间件Configure
方法如下:
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "resources")),
RequestPath = "/resources"
});
然后引用如下文件:
<link rel="stylesheet" href="~/resources/css/customer.css" />
return View()
保留用于返回视图执行的结果,即剃刀 CSHTML页。您不能使用 HTML 页面。尝试直接重定向:
return Redirect("~/resources/html/mentionslegales.html");
请参阅下面的链接,这可能会帮助您更好地了解如何从控制器返回静态 html 页面。
如何使用 ASP.NET Core MVC 发出 HTML 文件的 get 请求 https://stackoverflow.com/questions/49524917/how-to-make-a-get-request-for-an-html-file-using-asp-net-core-mvc
https://github.com/aspnet/Mvc/issues/3751 https://github.com/aspnet/Mvc/issues/3751
Update:
在View中注入静态html,可以参考:
控制器
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)
您也可以尝试上面第一个链接上建议的部分视图