Created
May 16, 2017 19:28
-
-
Save lordofscripts/2e6a15ccbe564f7b6ccafed31d9274b0 to your computer and use it in GitHub Desktop.
How to control Layout features in ASP.NET MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
// 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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