[英]ASP.NET web app force an iframe to reload
I'm not new to .NET Core development, but I am very new to ASP.NET Web Apps.我不是 .NET 核心开发的新手,但我是 ASP.NET Web 应用程序的新手。
I have an Index.cshtml that has the following iframe in it:我有一个 Index.cshtml,其中包含以下 iframe:
<iframe
height="800"
width="1300"
loading="eager"
frameBorder="0"
scrolling="no"
seamless="seamless"
allowtransparency="true"
src="https://example.com/?page_id=436"
title="My site" />
I need to force this iframe to reload (not from cache) whenever the page gets refreshed.每当页面刷新时,我都需要强制此 iframe 重新加载(而不是从缓存中加载)。 On the web, I found the following suggestion:
在web上,我发现了如下建议:
iframe.src = "https://example.com/?page_id=436?reload="+(new Date()).getTime();
Here's my question: Where in a ASP.NET web app (using bootstrap) would I put this code to make it refresh my iframe?这是我的问题:在 ASP.NET web 应用程序(使用引导程序)中,我会把这段代码放在什么地方来刷新我的 iframe? I'm somewhat familiar with javascript (haven't used it in a while), have very little knowledge of ASP.NET, and almost none of bootstrap.
我对 javascript 有点熟悉(有一段时间没用过),对 ASP.NET 知之甚少,对 bootstrap 几乎一无所知。 I know html/css pretty well.
我非常了解 html/css。
I'm confused about where in the code to put this bit of javascript so that it will always run when I refresh the page.我对将 javascript 这个位放在代码中的什么位置感到困惑,以便它在我刷新页面时始终运行。 And I don't know how to make that code reference my specific iframe.
而且我不知道如何使该代码引用我的特定 iframe。
Thanks!谢谢!
This bit of code refreshes your iframe
.这段代码刷新了你的
iframe
。 If you want to refresh it when the page is refreshed, you should execute it after the page loads — in a defer script:如果你想在页面刷新时刷新它,你应该在页面加载后执行它——在延迟脚本中:
<script defer>
iframe.src = "https://example.com/?page_id=436?reload="+(new Date()).getTime();
</script>
I was able to verify that the, as SNBS suggested, executed.我能够验证是否按照 SNBS 的建议执行了。 But for some reason, it didn't do what it needed to do.
但出于某种原因,它没有做它需要做的事情。 So, then I got an idea, Why not use the C# code instead of javascript. so now my Index:cshtml looks like this:
所以,然后我有了一个想法,为什么不使用 C# 代码而不是 javascript。所以现在我的 Index:cshtml 看起来像这样:
@page
@model IndexModel
@{
ViewData["Title"] = "Example";
string pageUrl = "https://example.com/?page_id=436?reload=" + DateTime.Now.ToString();
}
<div id="site-main">
<div>
<h1 class="headline">Printing some good stuff here</h1>
<p class="headline">And now some description!</p>
</div>
</div>
<div>
<iframe id="cgblog" height="800" width="1300" loading="eager" frameBorder="0" scrolling="no" seamless="seamless" allowtransparency="true" src="@pageUrl" title="My site">
</iframe>
</div>
The key was to define the variable pageUrl and then put that in the iframe's src.关键是定义变量 pageUrl,然后将其放入 iframe 的 src 中。 This works like a charm!
这就像一个魅力!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.