Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded code in Razor _Layout.cshtml

I'm working on an MVC3 Razor web application which gets it's page decoration from a java content management system. As this decoration is shared by every page I've put the retrieval of the CMS content in the _Layout.cshtml file but I'm not entirely happy with the code I've implemented...

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    @{
        -- The first two lines are temporary and will be removed soon.
        var identity = new GenericIdentity("", "", true);
        var principal = new GenericPrincipal(identity, new string[] { });
        var cmsInterface = MvcApplication.WindsorContainer.Resolve<ICMSInterface>();
        cmsInterface.LoadContent(principal, 2);
     }
     @Html.Raw(cmsInterface.GetHeadSection())
 </head>

<body>
    @Html.Raw(cmsInterface.GetBodySection(0))
    @RenderBody()
    @Html.Raw(cmsInterface.GetBodySection(1))
</body>
</html>

As there is no controller for the _layout file I can't see where else I could put the code to do the retrieval. Here are a few things that I've considered:

  • Retrieve the CMS content in separate pieces so I don't need the LoadContent call. Unfortunately, because of the component I have to use to retrieve the CMS content this isn't possible, it is all or nothing.
  • Use a partial view so I can utilise a controller. As I'd need to put the entire page into the partial that option just seems a bit ridiculous.
  • Call a single static method on some helper class which retrieves the data and adds the three sections to the ViewBag. That will allow me to move the code out of the view and feels like the best solution but I'm still not particularly happy with it.

Does anyone have any other suggestions/comments?

like image 688
DoctorMick Avatar asked Mar 16 '12 15:03

DoctorMick


People also ask

What are the elements included in the _layout Cshtml?

_Layout.cshtml The layout typically includes common user interface elements such as the app header, navigation or menu elements, and footer. Common HTML structures such as scripts and stylesheets are also frequently used by many pages within an app.

What is _layout Cshtml?

So, the _layout. cshtml would be a layout view of all the views included in Views and its subfolders. Setting Layout View in _ViewStart.cshtml. The _ViewStart. cshtml can also be created in the sub-folders of the View folder to set the default layout page for all the views included in that particular subfolder.

What is the difference between Cshtml and Razor?

razor helps you embed serverside code like C# code into web pages. cshtml is just a file extension. razor view engine is used to convert razor pages(. cshtml) to html.

What does the underscore on _layout Cshtml mean?

Layout pages are typically named _Layout. cshtml, the leading underscore preventing them from being browsed directly.


1 Answers

You can use a global action filter to add the required data to the ViewBag in all controllers:

public class LoadCmsAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        if (!filterContext.IsChildAction &&
            !filterContext.HttpContext.Request.IsAjaxRequest() &&
            filterContext.Result is ViewResult)
        {
            var identity = new GenericIdentity("", "", true);
            var principal = new GenericPrincipal(identity, new string[] { });
            var cmsInterface = MvcApp.WindsorContainer.Resolve<ICMSInterface>();
            cmsInterface.LoadContent(principal, 2);

            var viewBag = filterContext.Controller.ViewBag;
            viewBag.HeadSection = cmsInterface.GetHeadSection();
            viewBag.FirstBodySection = cmsInterface.BodySection(0);
            viewBag.SecondBodySection = cmsInterface.BodySection(1);
        }
    }
}

Global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    ...
    filters.Add(new LoadCmsAttribute());
}
like image 90
Balazs Tihanyi Avatar answered Sep 25 '22 06:09

Balazs Tihanyi