【问题标题】:MVC Controller Action Gets Called Multiple Times When Layout Is Specified指定布局时多次调用 MVC 控制器操作
【发布时间】:2012-12-10 09:54:43
【问题描述】:

我遇到了一个控制器操作至少被调用两次的问题。我有一个视图,它有另一个视图作为其布局页面,并且该视图被多次调用。如果我删除了布局规范,那么该操作将始终执行一次。我浏览了 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 = "TestMVCProject"; 布局=空; }

<!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。但是问题仍然存在,因为操作方法仍然被调用了两次......(编辑描述,因为我没有添加 cmets 的权限)

【问题讨论】:

  • 你的布局视图是什么样的?
  • 我刚刚更新了描述并添加了这个信息

标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

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

@Html.Partial("_Footer")

老实说,我不确定为什么布局会被调用两次,但我认为 @RenderPage()(我从来不需要)渲染整个 HTML 页面并注入结果。您必须在浏览器中检查页面来源才能确认。

HTH。

【讨论】:

    【解决方案2】:

    我发现了问题所在。代码中有这一行:

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

    src 将返回同一页面。一旦我用"" 替换它,它现在就可以正常工作了。

    【讨论】:

    • 如果可以的话,可以将此标记为答案,因为它仍然显示为未回答的问题。
    • 哇,哇,哇。那是什么PIA。就我而言,有人在整个代码库中嵌入了数十个这样的代码;复制/粘贴毫无疑问。这是我永远不会想到的。有趣的是,浏览器仅在问题被掩盖后才加载每个资源;如果是 10 次调用,它就不会那么微妙,也更容易被察觉。感谢您发布您的发现!
    【解决方案3】:

    我也遇到了同样的问题。 Action 渲染了两次。 唯一的问题是

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

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

    我刚改成""`

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

    现在一切正常。

    【讨论】:

    • &lt;iframe src="#" &gt;&lt;/iframe&gt; 可能有这个问题
    【解决方案4】:

    @Url.Content(...) 导致模型被我调用两次。也许其他一些问题也是如此

    【讨论】:

      【解决方案5】:

      当我们提供图像路径时也会发生这种情况,但是该路径上不存在图像。

      【讨论】:

        【解决方案6】:

        如果上述方法都不像我,请尝试禁用 Conveyor 扩展。

        那是我的噩梦。

        享受...

        【讨论】:

          猜你喜欢
          • 2017-11-22
          • 2014-11-10
          • 2011-07-22
          • 1970-01-01
          • 2011-02-12
          • 2015-01-28
          • 1970-01-01
          • 2012-07-06
          • 1970-01-01
          相关资源
          最近更新 更多