【问题标题】:iFrame error: Failed to load resource: the server responded with a status of 404 ()iFrame错误:加载资源失败:服务器响应状态为404()
【发布时间】:2021-07-23 19:54:53
【问题描述】:

我正在开发 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>

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

加载资源失败:服务器响应状态为404()

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

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

我该如何解决这个问题?

【问题讨论】:

    标签: html asp.net asp.net-mvc razor


    【解决方案1】:

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

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

    所以我做了一个返回视图的操作,它起作用了。

    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>

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

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

        这也应该是你放在 iFrame src 中的 url。

        【讨论】:

        • “直接链接”是什么意思? “HereIsTheiFrame.cshtml”的路径:C:\Folder1\Folder2\ProjectName\Views\FolderX\HereIsTheiFrame.cshtml; “HereIsTheContentForiFramel”的路径:C:\Folder1\Folder2\ProjectName\Views\FolderY\HereIsTheContentForiFrame.cshtml;
        • 你在浏览器中使用的url,是不是下面这个? 'localhost:44376/FolderY/HereIsTheContentForiFrame.cshtml'
        猜你喜欢
        • 1970-01-01
        • 2016-11-01
        • 2018-01-29
        • 2020-07-04
        • 1970-01-01
        • 2019-07-04
        • 2021-11-20
        相关资源
        最近更新 更多