当Layout被指定时,MVC控制器动作被多次调用

本文关键字:调用 控制器 MVC Layout 定时 | 更新日期: 2023-09-27 17:49:26

我有一个问题,其中一个控制器动作被调用至少两次。我有一个视图,它有另一个视图作为它的布局页面,这个视图被调用多次。如果我删除了布局的规范,那么该操作将始终执行一次。我浏览了StackOverflow和其他网站,但没有找到与我的问题具有相同特征的问题,所以我发布了一个新的问题。

_ViewStart.cshtml:
@{
   Layout = "~/Views/Shared/_ProfileLayout.cshtml";
}
Index.cshtml inside my Profile folder: @{
ViewBag.Title = "Index";    
}
Index
Controller Action:
public ActionResult Index()
    {            
        //ToDo: BusinessLogic
        //This method gets called twice
        //******************//
        return View();
    }  
这似乎是一个简单的问题,我肯定错过了一些明显的东西。我已经在这个网站上发布了示例项目:https://skydrive.live.com/#cid=F2DAB940147490B0&id=F2DAB940147490B0%21140

你知道怎么回事吗?

感谢

更新:视图如下:@ {ViewBag。Title = " testmvc项目";布局= null;}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TestMVCProject</title>
    <link rel="Stylesheet" type="text/css" href="../../Content/ActiveSite.css" />    
    <link href="../../Content/themes/TestMVCProject/jquery-ui-1.9.1.custom.min.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.8.2.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-ui-1.9.1.custom.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
  <div class="header">  
  <div id="loginimagecontainer">
  @using (Html.BeginForm("LoginActions", "Account", FormMethod.Post, new { @id = "LoginActionsForm" }))
  {  
  @Html.AntiForgeryToken()      
  <a href="#"><img src="/images/icons/message-icon.png" width="60" height="60" alt="Messages" title="Messages"/></a>
  <a href="/Account/LogOff"><img src="/images/icons/log-out-icon.png" width="60" height="60" alt="Log off" title="Log off"/></a>
  }    
  </div>
  <div class="logotext"><img alt="TestMVCProject Logo" src="#" width="350" height="150" id="TestMVCProjectLogo" /></div>
    </div>    
  <div class="content profile">  
    <div id="leftPane">
        <img src="#" alt="Placeholder" width="165" height="200" id="ProfilePhoto" title="Profile Photo" />
        <div id="Username"></div>
        <div id="NavLinks">
            <div class="ProfileNavigation" onclick="Navigate('/Profile/Update')"><span>Profile</span><img src="/images/icons/edit-icon.png" width="30" height="30" alt="Profile" /></div>
            <div class="ProfileNavigation"><span>Search</span><img src="/images/icons/search-icon.png" width="30" height="30" alt="Search" /></div>
            <div class="ProfileNavigation" onclick="Navigate('/Photo')"><span>Photos</span><img src="/images/icons/camera-icon.png" width="30" height="30" alt="Photos"/></div>
        </div>
    </div>
      <div id="adcontainer">
        <h4>Ads go here</h4>
        <p>content goes here</p>    
        </div>
    <div id="centerPane">
    @RenderBody()
    </div>     
  </div>
  @RenderPage("~/Views/Shared/_Footer.cshtml")
    <div id="redirectiondialog" class="dialog">
    <br />
    Hey, wait up... we're redirecting you over to the login page
    <br />
    </div> 
    <script type="text/javascript">    
        function Navigate(url) {
            window.location = url;
            return false;
        }
    </script>
</div>
</body>
</html>

这里是页脚页:

 <div class="footer">
    <div class="fltrt">Copyright 2012 TestMVCProject Inc&nbsp;</div>
    <p><a href="/Profile/Test">About</a> | <a href="#">Contact</a> | <a href="#">FAQ</a> | <a href="#">Advertise</a> | <a href="#">Support</a> | <a href="#">Feedback</a> | <a href="#">Login</a> | <a href="#">Register</a> | <a href="#">Privacy</a> | <a href="#">Terms</a></p>    
</div>

更新:@Tieson T:谢谢,我把它改成了Html。Partial而不是RenderPage。然而,问题仍然存在,因为action方法仍然被调用两次…(编辑描述,因为我没有权限添加评论)

当Layout被指定时,MVC控制器动作被多次调用

我找到了问题所在。代码中有这样一行:

<img alt="TestMVCProject Logo" src="#" width="350" height="150" id="TestMVCProjectLogo" />

src返回到同一页面。一旦我用""替换它,它现在工作得很好。

我也遇到过同样的问题。动作被渲染了两次。唯一的问题是

<img id="blah" src="#" alt="your image" /> 

不知道为什么图像的src属性导致在页面上执行两次渲染。

我只是把它改成" '

<img id="blah" src="" alt="your image" />

由于您的_Footer.cshtml视图只是普通的旧HTML,绝对没有理由调用@RenderPage()将其插入到您的布局中。使用@Html.Partial()代替:

@Html.Partial("_Footer")

老实说,我不知道为什么布局被调用两次,但我假设@RenderPage()(我从来没有需要过)渲染整个HTML页面并注入结果。您必须在浏览器中检查页面源代码来确认。

HTH .

@ url content(…)会导致模型被调用两次。可能还有其他问题

当我们给出一个图像路径,但是该路径上不存在图像时,也会发生这种情况

家伙的,如果没有以上工作像我一样,尝试禁用输送机扩展。

那是我的噩梦。

享受…