[英]How to return an embedded static html file from an Asp.Net Core Controller?
I have the following configuration: 我有以下配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDefaultFiles();
app.UseFileServer(new FileServerOptions
{
FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly, typeof(Startup).Namespace + ".WebApp"),
RequestPath = string.Empty
});
app.UseCors("CorsPolicy");
app.UseMvc(routes =>
{
routes.MapRoute(
name: "angular",
defaults: new {controller = "Angular", action = "Index"},
template: "{*url}");
});
}
My Angular project files are in the namespace MyNamespace.WebApp
. 我的Angular项目文件位于名称空间MyNamespace.WebApp
。
My AngularController
and Startup
classes are in the namespace MyNamespace
我的AngularController
和Startup
类位于名称空间MyNamespace
When I don't use MVC and access http://localhost:8000 it loads the index.html
file in the WebApp
folder. 当我不使用MVC并访问http:// localhost:8000时,它将在WebApp
文件夹中加载index.html
文件。 Now for all other requests (for example /action
) I have mapped it to the AngularController
, which looks as follows: 现在,对于所有其他请求(例如/action
),我已将其映射到AngularController
,其外观如下:
public class AngularController : Controller
{
public IActionResult Index() {
return View("/index.html");
}
}
I have debugged and verified that the request does come to AngularController.Index()
and returns View("/index.html")
. 我已经调试并验证了请求确实到达了AngularController.Index()
并返回View("/index.html")
。 But after that I get a 500 Error. 但是之后,我得到了500错误。 I'm guessing because it cannot find the view file. 我在猜测,因为它找不到视图文件。
How do I let MVC know to fetch the index.html
file from the embedded files? 如何让MVC知道从嵌入式文件中获取index.html
文件?
I've tried the following: 我尝试了以下方法:
return View("~/index.html");
return View("index.html");
return View("~/../index.html");
return View("../index.html");
return View("~/WebApp/index.html");
return View("WebApp/index.html");
None of these work. 这些都不起作用。 Probably I've missed a step? 可能我错过了一步?
In this example, I've tried to create an HTML file with the name htmlpage.html
in the path Views/Home/htmlpage.html
. 在此示例中,我尝试在路径Views/Home/htmlpage.html
创建一个名称为htmlpage.html
的HTML文件。
In the file Index.cshtml
(path: Views/Home/Index.cshtml
): 在文件Index.cshtml
(路径: Views/Home/Index.cshtml
):
@using Microsoft.AspNetCore.Hosting
@using System.IO
@inject IHostingEnvironment environment
@{
string htmlPath = "Views/Home/htmlpage.html";
}
@Html.Raw(File.ReadAllText(System.IO.Path.Combine(environment.ContentRootPath, htmlPath)))
In the file htmlpage.html
: 在htmlpage.html
文件中:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>bar</title>
</head>
<body>
<h3>foo</h3>
</body>
</html>
Test: 测试:
This way doesn't require: app.UseDefaultFiles();
这种方式不需要: app.UseDefaultFiles();
and app.UseFileServer(...);
和app.UseFileServer(...);
My solution was to configure a controller that would use an EmbeddedFileProvider
and fetch the contents of the embedded file as follows: 我的解决方案是配置一个控制器,该控制器将使用EmbeddedFileProvider
并按以下方式获取嵌入式文件的内容:
public AngularController: Controller
{
private IAppSettings appSettings;
public AngularController(IAppSettings appSettings)
{
this.appSettings = appSettings;
}
public IActionResult Index()
{
var fileProvider = new EmbeddedFileProvider(this.appSettings.WebAssembly, this.appSettings.WebNamespace);
var contents = this.fileProvider.GetDirectoryContents(string.Empty);
IFileInfo index = null;
foreach (var file in contents)
{
if (file.Name.Equals("index.html"))
{
index = file;
break;
}
}
if (index == null)
{
throw new Exception("'index.html' not found");
}
var reader = new StreamReader(index.CreateReadStream());
var text = reader.ReadToEnd();
return this.Content(text, "text/html");
}
}
The Assembly
and Namespace
of the embedded files are provided using dependency injection to make it more generic. 嵌入文件的Assembly
和Namespace
使用依赖项注入来提供,以使其更加通用。 You could also hard code it into the controller. 您也可以将其硬编码到控制器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.