iFrame error: Failed to load resource: the server responded with a status of 404 ()

iFrame error: Failed to load resource: the server responded with a status of 404 ()

我正在开发 ASP .Net MVC 项目,这是我第一次使用 iFrame。在一个文件夹中,我有我想要放置 iFrame 的视图,在另一个文件夹中,我有我想要放入框架中的内容。

结构如下:

Project
  -Folder X
  -Folder X
  ...
  -Views
    -FolderX
      -HereIsTheiFrame.cshtml
    -FolderY
      -HereIsTheContentForiFrame.cshtml
 

这是我试过的:

 <div>
     <iframe src="/Views/FolderY/HereIsTheContentForiFrame.cshtml">
     </iframe>
 </div>

但在浏览器控制台中出现错误:

Failed to load resource: the server responded with a status of 404 ()

如果我将其更改为 src="~/Views/FolderY/HereIsTheContentForiFrame.cshtml",我会收到此错误:

GET https://localhost:44376/Views/FolderY/HereIsTheContentForiFrame.cshtml 404

我该如何解决这个问题?

什么是直接 link 到“HereIsTheiFrame.cshtml”和“HereIsTheContentForiFrame.cshtml”?我怀疑它没有 /Views/ 文件夹。

这也应该是您放入 iFrame src 的 url。

ASP.net MVC 不允许您直接提供 .cshtml 页面,因为它希望您通过控制器提供它们。

确保您的 web.config 中包含以下内容以允许它直接提供 .cshtml:

  <appSettings>
    <add key="webpages:Enabled" value="true" />
  </appSettings>

根据错误,我看到它尝试进行 GET 调用:

GET https://localhost:44376/Views/FolderY/HereIsTheContentForiFrame.cshtml 404

所以我做了一个操作 returns 视图并且它起作用了。

public IActionResult Load_iFrameAsync()
{
   var model = new MyModel();
   return View("~/Views/FolderY/HereIsTheContentForiFrame.cshtml",model);
}

 <div>
     <iframe src="@Url.Action("Load_iFrame","MyControler")">
     </iframe>
 </div>