c# - 指定布局时 MVC Controller 操作被调用多次

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

我遇到了 Controller 操作至少被调用两次的问题。我有一个 View ,它的布局页面有另一个 View ,并且该 View 被多次调用。如果我删除布局规范,那么该操作将始终执行一次。我浏览了 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

有什么问题吗?

谢谢

更新: View 如下: @{ 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。然而,问题仍然存在,因为操作方法仍然被调用两次......(编辑描述,因为我没有添加评论的权限)

最佳答案

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

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

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

关于c# - 指定布局时 MVC Controller 操作被调用多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013505/

相关文章:

asp.net-mvc - 使用 Razor 防止 ASP.NET MVC 4 中 @Html.LabelFor 之后换行

c# - 为什么 libphonenumber 网站返回的结果与 libphonenumber 库不同?

asp.net - 将复选框组存储在@Html.HiddenFor中

javascript - 如何从 angularJs 连接 signalR

asp.net-mvc - windows azure 向时间字段添加 +10 小时

asp.net-mvc - 阻止从 URL 而不是 Ajax 调用访问部分 View

c# - 从类创建 UI

c# - VB.NET 与 C# 在控制台应用程序结束后没有显式释放任何内容而退出 Excel 有何不同?

c# - 将 C# 方法转换为将 SQL 标识符引用到 F#

c# - appSettings.json 中的自定义键在 ASP.NET Core 2.0 中不起作用