简体   繁体   English

在asp.net页面中嵌入html文件

[英]embed html files in an asp.net page

I have webpages that most of their content are created via plugins by accessing database many many times. 我有网页,他们的大多数内容是通过插件创建的,通过访问数据库很多次。 But I have considered that as the created result will always be the same (till user make changes to plugins), to make the html result each time the user makes changes and load the constant html each time page loads. 但我已经考虑过,因为创建的结果将始终相同(直到用户对插件进行更改),每次用户进行更改时都会生成html结果,并在每次加载页面时加载常量html。

This is What I have done: 这就是我所做的:

<%= System.IO.File.ReadAllText(Filename); %>

and in some pages I might have to read several files. 在某些页面中,我可能需要阅读几个文件。 Would this be better than creating pages each time or reading from files are slower? 这会比每次创建页面或从文件读取更慢更好吗?

what would you suggest? 你会建议什么?

What you need is caching and that can be implemented in many ways. 您需要的是缓存,可以通过多种方式实现。 Writing to temp files and updating those when the content changes is one way. 写入临时文件并在内容更改时更新这些文件是一种方法。 Using an in-memory cache is another and ASP.NET has that provided, also with expiration times, if required. 如果需要,使用内存缓存是另一个提供的ASP.NET,也提供了到期时间。 Also you can always use a combination of these techniques, for example, if you have lots of data that changes once a month but it's accessed very often. 此外,您可以始终使用这些技术的组合,例如,如果您有大量数据每月更改一次但是经常访问它们。 Then you could save the HTML into a file and on first request save it in memory cache also (with expiration). 然后,您可以将HTML保存到文件中,并在第一次请求时将其保存在内存缓存中(有效期)。

I'd say that usually loading a file is faster than doing database queries, but if you need to load a lot of files, then it might get slower. 我会说通常加载文件比执行数据库查询要快,但如果你需要加载大量文件,那么它可能会变慢。 Depends on the server system. 取决于服务器系统。 Memory cache will help in this case naturally and the server might also cache the file contents. 在这种情况下,内存缓存自然会有所帮助,服务器也可以缓存文件内容。

So it's not a simple yes/no or 'do this' answer for caching. 因此缓存不是一个简单的是/否或'做这个'的答案。 I'd suggest doing a search on the web for asp.net and caching, there probably is at least a similar case to yours somewhere. 我建议在网上搜索asp.net和缓存,在某些地方可能至少有类似的情况。

More information from MSDN: ASP.NET Caching: Techniques and Best Practises 来自MSDN的更多信息: ASP.NET缓存:技术和最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM