Asp.net mvc 如何将viewMasterPage链接到控制器

Asp.net mvc 如何将viewMasterPage链接到控制器,asp.net-mvc,asp.net-mvc-3,c#-4.0,razor,Asp.net Mvc,Asp.net Mvc 3,C# 4.0,Razor,我是MVC新手,我面临一个问题:如何将控制器“HelloWorld”连接到viewMasterPage“Index”上。。如果我希望我的所有页面都包含组件,我需要对所有视图使用viewMasterPage 编辑 我使用MVC3和Razor作为视图引擎这样做。我假设您使用的是web窗体视图引擎 母版页是视图/共享文件夹中的site.aspx文件。这是Views/HelloWorld文件夹中的索引文件 <%@ Page Title="" Language="C#" MasterPageFile

我是MVC新手,我面临一个问题:如何将控制器“HelloWorld”连接到viewMasterPage“Index”上。。如果我希望我的所有页面都包含组件,我需要对所有视图使用viewMasterPage

编辑
我使用MVC3和Razor作为视图引擎

这样做。我假设您使用的是web窗体视图引擎

母版页是视图/共享文件夹中的site.aspx文件。这是Views/HelloWorld文件夹中的索引文件

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Bla bla bla..
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index of HelloWord</h2>
    <%:ViewData["Message"]%>

</asp:Content>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<p>
     @ViewBag.Message
</p>
编辑:这是本书的剃刀版本

母版页是视图/共享文件夹中的_layout.cshtml文件。这是Views/HelloWorld文件夹中的your index.cshtml文件

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Bla bla bla..
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index of HelloWord</h2>
    <%:ViewData["Message"]%>

</asp:Content>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<p>
     @ViewBag.Message
</p>
以下是控制器文件夹中的HelloWorldController.cs文件的外观

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace App.Controllers {

    public class HelloWorldController : Controller {

        public ActionResult Index() {

            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace App.Controllers {

    public class HelloWorldController : Controller {

        public ActionResult Index() {

            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

    }
}

注意:这里我假设您的路由是visual studio在创建新的MVC internet应用程序时为您创建的默认路由。母版页的工作方式类似于模板文件,大多数部分是静态的,您只需更改几个位置即可显示所需的结果。视图引擎动态生成不同视图页面的变化部分。生成动态内容的操作称为“渲染”

当使用Razor View引擎时,您需要知道哪些部件是静态的,哪些部件正在更改。对于变化的部分,除了一个神奇的调用
@RenderBody()
对于某些视图中需要但其他视图中不需要的零件。使用
@RenderSection(“sectionName”,false)

下面是一个示例_layout.cshtml文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   ... your style sheet files ... 
</head>

<body>
     ... shared components...     
    @RenderBody()
     ...I like to put the java script files to the end...     
    @RenderSection("extraScripts", required: false)
</body>
</html>
在文件的开头。视图文件的其余部分将由
@RenderBody()
调用。如果要为视图添加一些自定义脚本,可以通过

@section extraScripts{
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">      </script>
}
@节外部脚本{
}

为该页面添加的额外脚本将在运行时呈现到结果页面。

是否直接查看母版页?我觉得这不合理?你用的是哪个版本的MVC。你的视图引擎是什么?razor、web表单视图引擎或任何其他?MVC3和yes razor作为视图引擎我发布了一个答案,但我不确定我是否正确理解您。希望我做到了:)这不是打开URL\HelloWorld上的母版页\Index@Sheza显然我没有在这里跟踪你\HelloWorld\Index视图与此处的app.controllers.HelloWorld.Index()操作相关。你可以看到我的代码,它有一个布局参考,我认为如果你使用Razor,你应该调用它而不是母版页。当然,路由引擎找不到HelloWorld控制器。按照惯例,MVC中的所有控制器都应该以“Controller”一词结尾。所以它应该被命名HelloWorldController@Hakeem说得好。你完全正确。我在这件事上一直马虎。我更新了答案。