Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to control Layout features in ASP.NET MVC
@{
// Control Layout Features at View Level
Models.Misc.LayoutOptions LayoutOpts = ViewBag.LayoutOptions ?? new Models.Misc.LayoutOptions();
}
<!DOCTYPE html>
<html>
<head itemscope itemtype="http://schema.org/WebPage">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title itemprop="name">@ViewData["Title"]</title>
<environment names="Development">
@if (LayoutOpts.HasMap)
{
<script async defer src="https://maps.googleapis.com/maps/api/js?key=XXXXXXX" type="text/javascript"></script>
}
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/lib/font-awesome/css/font-awesome.css" type="text/css" media="all">
<link rel="stylesheet" href="~/lib/font-awesome/fonts/fontawesome-webfont.ttf" type="text/css" media="all">
<link rel="stylesheet" href="~/css/site.css" />
@if (LayoutOpts.HasLightBox) [
<link rel="stylesheet" href="~/lib/lightbox2/dist/css/lightbox.css" />
}
@if (LayoutOpts.HasMap) {
<link rel="stylesheet" href="~/css/googleMap.css" />
}
</environment>
<environment names="Staging,Production">
@if (LayoutOpts.HasMap) {
<script async defer src="https://maps.googleapis.com/maps/api/js?key=XXXXX" type="text/javascript"></script>
}
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
<link rel="stylesheet" href="~/lib/font-awesome/css/font-awesome.min.css" type="text/css" media="all">
<link rel="stylesheet" href="~/lib/font-awesome/fonts/fontawesome-webfont.ttf" type="text/css" media="all">
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
<link rel="stylesheet" href="~/css/bootstrap-social.min.css" />
@if (LayoutOpts.HasLightBox) {
<link rel="stylesheet" href="~/lib/lightbox2/dist/css/lightbox.min.css" />
}
@if (LayoutOpts.HasMap) {
<link rel="stylesheet" href="~/css/googleMap.min.css" />
}
</environment>
@Html.Raw(JavaScriptSnippet.FullScript)
</head>
<body>
@RenderSection("Scripts", required: false)
@if (LayoutOpts.HasLightBox) {
<script src="~/lib/lightbox2/dist/js/lightbox.min.js"></script>
}
</body>
</html>
/// <summary>
/// Application features that need rendering control at Layout level (used in ~/Shared/_Layout.cshtml
/// </summary>
public class LayoutOptions
{
/// <summary>
/// get/set whether Layout will render Google Map support features
/// </summary>
public bool HasMap { get; set; }
/// <summary>
/// get/set whether Layout will render LightBox (image gallery) support features
/// </summary>
public bool HasLightBox { get; set; }
#region Ctor
/// <summary>
/// Default Constructor. Here we set all properties to their default value
/// </summary>
public LayoutOptions()
{
HasMap = false;
HasLightBox = false;
}
#endregion
}
public class MapController : Controller
{
public ActionResult Index()
{ // map controller view, therefore let Layout render map-related stuff
ViewBag.LayoutOptions = new PanamaVibes.Mvc.Models.Misc.LayoutOptions
{
HasMap = true
};
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment