如何确保 iframe 加载到 ASP.Net Core Razor
How can ensure that iframe loaded in ASP.Net Core Razor
我有一个简单的项目 ASP.Net Core with Razor。在这个项目中,我有一个只打开 iframe 的页面。这很好用,但我想知道什么时候没有加载 iframe,这可能吗?
@page
@using Microsoft.AspNetCore.Http.Extensions
@model IndexModel
<iframe src="@Model.BuildIFrameReference(HttpContext.Request.GetDisplayUrl())"
frameborder="0"
style="overflow: hidden;overflow-x: hidden;overflow-y: hidden;height: 100%;width: 100%;position: absolute;top: 0px;left: 0px;right: 0px;bottom: 0px"
height="100%"
width="100%"/>
您可以查看iframe的加载事件。
<iframe id="myFrame" src=...></iframe>
@section Scripts
{
<script>
$('#myFrame').on('load', function () {
var iframeBody = this.contentDocument.body;
console.log('iframe loaded, body is: ', iframeBody);
alert("iframe loaded");
});
</script>
}
所以我选择不同的方式来做 this.I 添加新的中间件来 ping iframe url。如果 iframe 的 url 返回未成功状态代码或具有 X-Frame-Options:拒绝 header 我将重定向到默认页面。
public class CheckIframeUrlMiddleware
{
private readonly HttpClient _client;
private readonly RequestDelegate _next;
public CheckIframeUrlMiddleware(RequestDelegate next)
{
_next = next;
_client = new HttpClient();
}
public async Task InvokeAsync(HttpContext context, IframeReferenceBuilder _builder)
{
try
{
var response = await _client.GetAsync(_builder.BuildIFrameReference(context.Request.GetDisplayUrl()));
if (!response.IsSuccessStatusCode)
context.Response.Redirect(_builder.Settings.RedirectDomain);
if (response.Headers.TryGetValues("X-Frame-Options", out var values) && values.Contains("DENY"))
context.Response.Redirect(_builder.Settings.RedirectDomain);
await _next.Invoke(context);
}
catch
{
context.Response.Redirect(_builder.Settings.RedirectDomain);
}
}
}
我有一个简单的项目 ASP.Net Core with Razor。在这个项目中,我有一个只打开 iframe 的页面。这很好用,但我想知道什么时候没有加载 iframe,这可能吗?
@page
@using Microsoft.AspNetCore.Http.Extensions
@model IndexModel
<iframe src="@Model.BuildIFrameReference(HttpContext.Request.GetDisplayUrl())"
frameborder="0"
style="overflow: hidden;overflow-x: hidden;overflow-y: hidden;height: 100%;width: 100%;position: absolute;top: 0px;left: 0px;right: 0px;bottom: 0px"
height="100%"
width="100%"/>
您可以查看iframe的加载事件。
<iframe id="myFrame" src=...></iframe>
@section Scripts
{
<script>
$('#myFrame').on('load', function () {
var iframeBody = this.contentDocument.body;
console.log('iframe loaded, body is: ', iframeBody);
alert("iframe loaded");
});
</script>
}
所以我选择不同的方式来做 this.I 添加新的中间件来 ping iframe url。如果 iframe 的 url 返回未成功状态代码或具有 X-Frame-Options:拒绝 header 我将重定向到默认页面。
public class CheckIframeUrlMiddleware
{
private readonly HttpClient _client;
private readonly RequestDelegate _next;
public CheckIframeUrlMiddleware(RequestDelegate next)
{
_next = next;
_client = new HttpClient();
}
public async Task InvokeAsync(HttpContext context, IframeReferenceBuilder _builder)
{
try
{
var response = await _client.GetAsync(_builder.BuildIFrameReference(context.Request.GetDisplayUrl()));
if (!response.IsSuccessStatusCode)
context.Response.Redirect(_builder.Settings.RedirectDomain);
if (response.Headers.TryGetValues("X-Frame-Options", out var values) && values.Contains("DENY"))
context.Response.Redirect(_builder.Settings.RedirectDomain);
await _next.Invoke(context);
}
catch
{
context.Response.Redirect(_builder.Settings.RedirectDomain);
}
}
}