简体   繁体   English

ASP.Net Core - 动态生成 index.html

[英]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:您应该执行以下操作:

  1. update your usage of UseStaticFiles , it should have a lambda provided for the OnPrepareResponse property;更新您对UseStaticFiles的使用,它应该为OnPrepareResponse属性提供 lambda; See this MS DOC请参阅此MS DOC
  2. It is called every time the static file is returned to the client; static文件每次返回给客户端时调用;
  3. You can analyze the file name to ensure it is index.html;您可以分析文件名,确保它是index.html;
  4. Your code can completely change the content that will be returned.您的代码可以完全更改将返回的内容。 Just generate a file dynamically to replace index.html, and write it as a response body by writing to a stream. You might need to clear or save original body content before modifying and returning it if your writing has failed.只需要动态生成一个文件替换index.html,写入一个stream作为响应body即可。如果写入失败,可能需要清空或保存原body内容再修改返回。

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.

相关问题 带有 React 模板的 ASP.NET Core 返回 index.html - ASP.NET Core with React template returns index.html 在 asp.net core 中将 index.html 设置为默认页面 - Setting index.html as default page in asp.net core 如何使用 controller 或在 ASP.NET Core MVC 中查看 wwwroot 文件夹中的 index.html 文件 - How to render index.html file that is in wwwroot folder using controller or view in ASP.NET Core MVC ASP.NET 5项目不提供index.html - ASP.NET 5 Project not serving index.html 在ASP.NET中动态生成HTML - Dynamically Generate HTML in ASP.NET 带有角度模板的 asp.net core 3.1 无法再响应 404 并始终重定向到 spa index.html 页面 - asp.net core 3.1 with angular template can no longer respond with 404 and always redirect to spa index.html page Asp.Net Core动态生成图像文件 - Asp.Net Core Generate Image file dynamically ASP.NET使用服务器控件动态生成HTML - ASP.NET dynamically generate HTML with server controls 如何在ASP.NET中动态生成HTML和CSS? - How to generate HTML and CSS dynamically in asp.net? 如何在TFS 2017 Build之后通过TFS Build Job中的Shell脚本自动递增asp.net核心应用程序版本并将其显示在index.html页脚中 - How to auto increment asp.net core application version via shell script in TFS Build Job after TFS 2017 Build and show it in the footer of index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM