简体   繁体   English

ASP.Net MVC生成DNS预取标记

[英]ASP.Net MVC Generate DNS Prefetch Tags

Is there a way to find all the src="" urls when rendering a ASP.net MVC page in the view to then generate DNS prefetch tags on the fly? 有没有办法在视图中呈现ASP.net MVC页面时找到所有src="" url,然后动态生成DNS预取标记?

https://www.chromium.org/developers/design-documents/dns-prefetching https://www.chromium.org/developers/design-documents/dns-prefetching

If I understood correctly I can tell you the following: 如果我理解正确,我可以告诉你以下内容:

Option #1: (Not a pretty solution but would work.) 选项#1 :(不是一个漂亮的解决方案,但会起作用。)

NOTE: for this try to use simple Javascript and not rely on JQuery or other (since then you still need to "load" the .JS file for that and that is ruining the point of your question. 注意:为此尝试使用简单的Javascript而不依赖于JQuery或其他(从那时起你仍然需要“加载”.JS文件,这会破坏你的问题。

Process your src/href or some other predefined property tag with some kind of "OwnLogic" to define the "base target", but in a way that the browser would not be able to initiate the request to obtain that image or other file. 使用某种“OwnLogic”处理您的src/href或其他预定义属性标记以定义“基本目标”,但是浏览器无法启动获取该图像或其他文件的请求。 Example: 例:

<img url="" class="DNS_BaseTarget" DNS_BaseTarget="smiley.gif||myCDNPointerInfo" alt="">

Then, with javascript, get a list of all elements that uses the class DNS_BaseTarget and then read the property value and update the "src" tag. 然后,使用javascript,获取使用类DNS_BaseTarget的所有元素的列表,然后读取属性值并更新“src”标记。 At the same time you can inject by javascript inject all the ' <link rel="dns-prefetch" href="https://cdn.yourTargetDomain.com"> ' that you will use based on the information you just processed. 同时你可以通过javascript注入你将根据刚刚处理的信息使用的所有' <link rel="dns-prefetch" href="https://cdn.yourTargetDomain.com"> '。

I did not tested this concept, so "lag" or some sort of delay in the client might be expected (but maybe not noticeble by the user). 我没有测试过这个概念,因此可能会预期客户端出现“滞后”或某种延迟(但用户可能没有注意到)。

Option #2: 选项#2:

The View Result Execution Process (in MVC life cycle) tell us that the method 'Render()' is the last one to be executed. 视图结果执行过程(在MVC生命周期中)告诉我们方法'Render()'是最后一个要执行的方法。 With this being said, you can create your own custom override logic Example: intercept view rendering to add HTML/JS on all partial views? 有了这个,你可以创建自己的自定义覆盖逻辑示例:截取视图渲染以在所有部分视图上添加HTML / JS? How to intercept view rendering to add HTML/JS on all partial views? 如何拦截视图渲染以在所有局部视图上添加HTML / JS?

With this concept of trying to "process" the final html before sending it to the user, you could somehow "parse" the file.... try to get all the 'src/href' and then inject all the ' <link rel="dns-prefetch" href="https://cdn.yourTargetDomain.com"> ' that you will use. 有了这个概念,试图在发送给用户之前“处理”最终的html,你可以以某种方式“解析”文件....尝试获取所有'src / href',然后注入所有' <link rel="dns-prefetch" href="https://cdn.yourTargetDomain.com">你将使用的<link rel="dns-prefetch" href="https://cdn.yourTargetDomain.com"> '。

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

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