C# 将viewBag返回母版页MVC Razor

C# 将viewBag返回母版页MVC Razor,c#,asp.net-mvc-4,razor,controller,master-pages,C#,Asp.net Mvc 4,Razor,Controller,Master Pages,我想将ViewBag返回到我的母版页,但我对MVC Razor没有太多经验,因此我不知道如何解决我的问题,以下是我的代码: 控制器: MasterController.cs public class SharedController : Controller { public ActionResult SiteMaster() { ViewBag.PageTitle = "Show this text into my MasterPage"; re

我想将
ViewBag
返回到我的母版页,但我对MVC Razor没有太多经验,因此我不知道如何解决我的问题,以下是我的代码:

控制器:

MasterController.cs

public class SharedController : Controller
{
    public ActionResult SiteMaster()
    {
        ViewBag.PageTitle = "Show this text into my MasterPage";
        return View();
    }
}

HomeController.cs

    public class HomeController : Controller
    {
      public ActionResult Index()
      {
        return View();
      }
    }
public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
观点:

共享->
SiteMaster.cshtml

<html>
 <head></head>
 <body>
 <div>@ViewBag.PageTitle</div>  //Doesn't work
 <div>@RenderBody()</div>
 </body>
</html>
@{
  Layout = "~/Views/Shared/SiteMaster.cshtml";
 } 
 <div>Test</div>
应用程序启动->
RouteConfig.cs

    public class HomeController : Controller
    {
      public ActionResult Index()
      {
        return View();
      }
    }
public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

我相信您只需将viewbag分配放入homecontroller的索引操作中即可。您的sitemaster操作未被调用。

您可以使用全局操作筛选器执行此操作:

public class MasterPageTitleFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.Controller.ViewBag.PageTitle = "Show this text into my MasterPage";
        base.OnActionExecuted(filterContext);
    }
}
如果将此筛选器设置为操作,则在生成视图之前,它将在结束时触发,以便在
ViewBag
中添加标题

为了在不标记所有操作/控制器的情况下将其用于每个操作/控制器,请将其添加到全局过滤器注册中:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    // other filters to add
    filters.Add(new MasterPageTitleFilterAttribute());
}

您可以创建一个包含重写方法的基本控制器来执行此操作

public class BaseController: Controller
{
    public override void OnActionExecuting(ActionExecutedContext filterContext)
        {
            filterContext.Controller.ViewBag.PageTitle = "Show this text into my MasterPage";
            //base.OnActionExecuted(filterContext);
        }
}
然后,您将从BaseController继承其他控制器。因此,您每次都会在母版页中访问ViewBag.PageTitle

public class HomeController : BaseController
{
...
}

您需要在操作中指定viewbag

public class HomeController : Controller
{
      public ActionResult Index()
      {
        ViewBag.PageTitle = "Show this text into my MasterPage";
        return View();
      }
}

你想在每次显示视图时都显示
ViewBag.PageTitle
?是的,因为我有母版页,所以我需要在包含
母版页的每个页面上显示
视图包
。我刚刚尝试过,但它不起作用,但是,我需要仅在母版页布局上显示该视图包,并与其他页面分开示例中有代码有时会覆盖cshtml文件中的viewbag项目-可能会检查它是否在razor中以某种方式被重置?当您将viewbag分配放在索引操作中并尝试在index.cshtml中显示它时,会发生什么情况?它是否至少做到了这一点?仅供参考-我尝试了您在新项目中描述的内容,但效果良好(即索引操作中的赋值)。错误1'Belina.Controllers.BaseController.OnActionExecuted(System.Web.Mvc.ActionExecutedContext)':重写“受保护的”继承成员“System.Web.Mvc.Controller.OnActionExecuted”时无法更改访问修饰符(System.Web.Mvc.ActionExecuteContext)“好吧,你说得对,我试过了,我错过了一些我编辑过的答案,不是OnActionExecuted它一定是OnActionExecuted它工作了。