Skip to content

Instantly share code, notes, and snippets.

View scottaddie's full-sized avatar
💭
.NET 💜 Azure

Scott Addie scottaddie

💭
.NET 💜 Azure
View GitHub Profile
@scottaddie
scottaddie / CardTagHelper.cs
Last active January 30, 2017 04:08
Unmanageable HTML concatentation
output.Content.SetHtmlContent(
$"<img src=\"images/{handContext.Player}.png\" alt=\"avatar\" class=\"center-block\" /><div class=\"text-center\"><h2 class=\"{suitAttributes.colorClass}\"><strong>{suitAttributes.characterCode}</strong></h2><p>{Rank}</p></div>");
@scottaddie
scottaddie / CardTagHelper.cs
Last active January 30, 2017 03:42
Retrieve the custom HandContext object
// Fetch the context, so that we can get the player name to display the appropriate image
var handContext = (HandContext)context.Items[typeof(HandTagHelper)];
var playerName = handContext.Player;
@scottaddie
scottaddie / HandTagHelper.cs
Last active January 30, 2017 13:45
Custom context class for communicating from parent to child tag helper
public class HandContext
{
public string Player { get; set; }
}
[HtmlTargetElement(Constants.HAND_TAG_HELPER_ELEMENT_NAME, Attributes = nameof(Player), TagStructure = TagStructure.NormalOrSelfClosing)]
[RestrictChildren(Constants.CARD_TAG_HELPER_ELEMENT_NAME)]
public class HandTagHelper : TagHelper
{
public string Player { get; set; }
@scottaddie
scottaddie / Index.cshtml
Last active January 29, 2017 03:03
Tag helper markup to test HtmlTargetElement "AND" operation
<hand>
<card suit="Spade" rank="Queen"></card> <!-- TagHelper -->
<card rank="Queen"></card> <!-- Not a TagHelper -->
<card suit="Spade"></card> <!-- Not a TagHelper -->
</hand>
@scottaddie
scottaddie / CardTagHelper.cs
Last active January 29, 2017 03:03
CardTagHelper HtmlTargetElement attribute "AND" operation
[HtmlTargetElement("card", ParentTag = "hand", Attributes = nameof(Suit) + "," + nameof(Rank), TagStructure = TagStructure.NormalOrSelfClosing)]
public class CardTagHelper : TagHelper
{
public string Rank { get; set; }
public string Suit { get; set; }
}
@scottaddie
scottaddie / Index.cshtml
Last active January 29, 2017 03:02
Tag helper markup to test HtmlTargetElement "OR" operation
<hand>
<card suit="Diamond"></card> <!-- TagHelper -->
<card rank="Four"></card> <!-- TagHelper -->
<card></card> <!-- Not a TagHelper -->
</hand>
@scottaddie
scottaddie / CardTagHelper.cs
Last active January 29, 2017 03:04
CardTagHelper HtmlTargetElement attribute "OR" operation
[HtmlTargetElement("card", ParentTag = "hand", Attributes = nameof(Suit), TagStructure = TagStructure.NormalOrSelfClosing)]
[HtmlTargetElement("card", ParentTag = "hand", Attributes = nameof(Rank), TagStructure = TagStructure.NormalOrSelfClosing)]
public class CardTagHelper : TagHelper
{
public string Rank { get; set; }
public string Suit { get; set; }
}
@scottaddie
scottaddie / Index.cshtml
Last active January 31, 2017 03:44
Custom tag helpers being used in an ASP.NET Core MVC Razor view
@using static TagHelpersDemo.TagHelpers.CardTagHelper
@{
ViewData["Title"] = "Home Page";
}
<div class="clearfix">&nbsp;</div>
<hand player="John">
<card suit="@CardSuit.Heart" rank="@CardRank.Ace"></card>
@scottaddie
scottaddie / MessyClassLibrary.csproj
Created October 1, 2016 02:02
BeforeBuild MSBuild target with a condition checking IsLocalDev variable
<PropertyGroup>
<IsLocalDev>true</IsLocalDev>
</PropertyGroup>
<Target Name="BeforeBuild" Condition="$(IsLocalDev) == true">
<Exec Command="CodeFormatter $(MSBuildProjectFile) /rule+:BraceNewLine,UsingLocation,FormatDocument,NewLineAbove /rule-:Copyright,CustomCopyright,UnicodeLiterals,ExplicitVisibility,IllegalHeaders,ExplicitThis,ReadonlyFields,FieldNames /verbose"
ContinueOnError="false" />
</Target>
<Target Name="BeforeBuild">
<Exec Command="CodeFormatter $(MSBuildProjectFile) /rule+:BraceNewLine,UsingLocation,FormatDocument,NewLineAbove /rule-:Copyright,CustomCopyright,UnicodeLiterals,ExplicitVisibility,IllegalHeaders,ExplicitThis,ReadonlyFields,FieldNames /verbose"
ContinueOnError="false" />
</Target>