[英]ASP.Net Core - Dynamically generate index.html
The standard way to provide an index.html to the client with ASP.Net core is to use the middleware app.UseStaticFiles();
使用 ASP.Net Core 向客户端提供 index.html 的标准方法是使用中间件
app.UseStaticFiles();
on IApplicationBuilder
instance in the Configure
method of the StartUp
class. This provides a static index.html file from the wwwroot folder.在
StartUp
class 的Configure
方法中的IApplicationBuilder
实例上。这提供了 wwwroot 文件夹中的 static index.html 文件。 Is there a way to provide an index.html to the client, that is dynamically generated in code on request?有没有办法向客户端提供 index.html,它是根据请求在代码中动态生成的?
There is a way to change the SPA files content served from the static folder.有一种方法可以更改从 static 文件夹提供的 SPA 文件内容。
You should do the following:您应该执行以下操作:
UseStaticFiles
, it should have a lambda provided for the OnPrepareResponse
property;UseStaticFiles
的使用,它应该为OnPrepareResponse
属性提供 lambda; See this MS DOC See code example to get you started:请参阅代码示例以帮助您入门:
var staticFileOptions = new StaticFileOptions
{
OnPrepareResponse = context =>
{
// Only for Index.HTML file
if (context.File.Name == "index.html") {
var response = context.Context.Response;
var str = "This is your new content";
var jsonString = JsonConvert.SerializeObject(str);
// modified stream
var responseData = Encoding.UTF8.GetBytes(jsonString);
var stream = new MemoryStream(responseData);
// set the response body
response.Body = stream;
}
}
};
app.UseStaticFiles(staticFileOptions);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.