本文整理汇总了C#中Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent.AppendHtml方法的典型用法代码示例。如果您正苦于以下问题:C# DefaultTagHelperContent.AppendHtml方法的具体用法?C# DefaultTagHelperContent.AppendHtml怎么用?C# DefaultTagHelperContent.AppendHtml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent
的用法示例。
在下文中一共展示了DefaultTagHelperContent.AppendHtml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetHtmlContent_ClearsExistingContent
public void SetHtmlContent_ClearsExistingContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendHtml("Contoso");
// Act
tagHelperContent.SetHtmlContent("Hello World!");
// Assert
Assert.Equal("Hello World!", tagHelperContent.GetContent(new HtmlTestEncoder()));
}
示例2: ProcessAsync
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (ShowDismiss)
{
output.PreContent.AppendFormat(@"<button type='button' class='btn btn-default' data-dismiss='modal'>{0}</button>", DismissText);
}
var childContent = await output.GetChildContentAsync();
var footerContent = new DefaultTagHelperContent();
if (ShowDismiss)
{
footerContent.AppendFormat(@"<button type='button' class='btn btn-default' data-dismiss='modal'>{0}</button>", DismissText);
}
footerContent.AppendHtml(childContent);
var modalContext = (ModalContext)context.Items[typeof(ModalTagHelper)];
modalContext.Footer = footerContent;
output.SuppressOutput();
}
示例3: CopyTo_CopiesAllItems
public void CopyTo_CopiesAllItems()
{
// Arrange
var source = new DefaultTagHelperContent();
source.AppendHtml(new HtmlEncodedString("hello"));
source.Append("Test");
var items = new List<object>();
var destination = new HtmlContentBuilder(items);
destination.Append("some-content");
// Act
source.CopyTo(destination);
// Assert
Assert.Equal(3, items.Count);
Assert.Equal("some-content", Assert.IsType<string>(items[0]));
Assert.Equal("hello", Assert.IsType<HtmlEncodedString>(items[1]).Value);
Assert.Equal("Test", Assert.IsType<string>(items[2]));
}
示例4: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput(
object model,
Type containerType,
Func<object> modelAccessor,
string propertyPath,
TagHelperOutputContent tagHelperOutputContent)
{
// Arrange
var expectedTagName = "not-label";
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
{ "for", tagHelperOutputContent.ExpectedId }
};
var metadataProvider = new TestModelMetadataProvider();
var containerMetadata = metadataProvider.GetMetadataForType(containerType);
var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, model);
var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, "Text");
var modelExplorer = containerExplorer.GetExplorerForExpression(propertyMetadata, modelAccessor());
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
var modelExpression = new ModelExpression(propertyPath, modelExplorer);
var tagHelper = new LabelTagHelper(htmlGenerator)
{
For = modelExpression,
};
var expectedPreContent = "original pre-content";
var expectedPostContent = "original post-content";
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList(),
items: new Dictionary<object, object>(),
uniqueId: "test");
var htmlAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
htmlAttributes,
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendHtml(tagHelperOutputContent.OriginalChildContent);
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
output.PreContent.AppendHtml(expectedPreContent);
output.PostContent.AppendHtml(expectedPostContent);
// LabelTagHelper checks IsContentModified so we don't want to forcibly set it if
// tagHelperOutputContent.OriginalContent is going to be null or empty.
if (!string.IsNullOrEmpty(tagHelperOutputContent.OriginalContent))
{
output.Content.AppendHtml(tagHelperOutputContent.OriginalContent);
}
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
tagHelper.ViewContext = viewContext;
// Act
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(
tagHelperOutputContent.ExpectedContent,
HtmlContentUtilities.HtmlContentToString(output.Content));
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode);
Assert.Equal(expectedTagName, output.TagName);
}
示例5: RenderLinkTags_FallbackHref_WithFileVersion_EncodesAsExpected
public void RenderLinkTags_FallbackHref_WithFileVersion_EncodesAsExpected()
{
// Arrange
var expectedContent = "<link encoded=\"contains \"quotes\"\" " +
"href=\"HtmlEncode[[/css/site.css?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\" " +
"literal=\"HtmlEncode[[all HTML encoded]]\" " +
"mixed=\"HtmlEncode[[HTML encoded]] and contains \"quotes\"\" />" +
Environment.NewLine +
"<meta name=\"x-stylesheet-fallback-test\" content=\"\" class=\"HtmlEncode[[hidden]]\" />" +
"<script>!function(a,b,c){var d,e=document,f=e.getElementsByTagName(\"SCRIPT\")," +
"g=f[f.length-1].previousElementSibling," +
"h=e.defaultView&&e.defaultView.getComputedStyle?e.defaultView.getComputedStyle(g):g.currentStyle;" +
"if(h&&h[a]!==b)for(d=0;d<c.length;d++)e.write('<link rel=\"stylesheet\" href=\"'+c[d]+'\"/>')}(" +
"\"JavaScriptEncode[[visibility]]\",\"JavaScriptEncode[[hidden]]\"," +
"[\"JavaScriptEncode[[HtmlEncode[[/fallback.css?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]]]\"]);" +
"</script>";
var mixed = new DefaultTagHelperContent();
mixed.Append("HTML encoded");
mixed.AppendHtml(" and contains \"quotes\"");
var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList
{
{ "asp-append-version", "true" },
{ "asp-fallback-href-include", "**/fallback.css" },
{ "asp-fallback-test-class", "hidden" },
{ "asp-fallback-test-property", "visibility" },
{ "asp-fallback-test-value", "hidden" },
{ "encoded", new HtmlString("contains \"quotes\"") },
{ "href", "/css/site.css" },
{ "literal", "all HTML encoded" },
{ "mixed", mixed },
});
var output = MakeTagHelperOutput(
"link",
attributes: new TagHelperAttributeList
{
{ "encoded", new HtmlString("contains \"quotes\"") },
{ "literal", "all HTML encoded" },
{ "mixed", mixed },
});
var hostingEnvironment = MakeHostingEnvironment();
var viewContext = MakeViewContext();
var globbingUrlBuilder = new Mock<GlobbingUrlBuilder>(
new TestFileProvider(),
Mock.Of<IMemoryCache>(),
PathString.Empty);
globbingUrlBuilder.Setup(g => g.BuildUrlList(null, "**/fallback.css", null))
.Returns(new[] { "/fallback.css" });
var helper = new LinkTagHelper(
MakeHostingEnvironment(),
MakeCache(),
new HtmlTestEncoder(),
new JavaScriptTestEncoder(),
MakeUrlHelperFactory())
{
AppendVersion = true,
FallbackHrefInclude = "**/fallback.css",
FallbackTestClass = "hidden",
FallbackTestProperty = "visibility",
FallbackTestValue = "hidden",
GlobbingUrlBuilder = globbingUrlBuilder.Object,
Href = "/css/site.css",
ViewContext = viewContext,
};
// Act
helper.Process(context, output);
// Assert
Assert.Equal("link", output.TagName);
Assert.Equal("/css/site.css?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["href"].Value);
var content = HtmlContentUtilities.HtmlContentToString(output, new HtmlTestEncoder());
Assert.Equal(expectedContent, content);
}
示例6: ProcessAsyncInTemplate_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList
public async Task ProcessAsyncInTemplate_WithItems_GeneratesExpectedOutput_DoesNotChangeSelectList(
object model,
Type containerType,
Func<object> modelAccessor,
NameAndId nameAndId,
string expectedOptions)
{
// Arrange
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var originalPostContent = "original content";
var expectedAttributes = new TagHelperAttributeList(originalAttributes)
{
{ "id", nameAndId.Id },
{ "name", nameAndId.Name },
{ "valid", "from validation attributes" },
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = originalPostContent + expectedOptions;
var expectedTagName = "select";
var metadataProvider = new TestModelMetadataProvider();
var containerMetadata = metadataProvider.GetMetadataForType(containerType);
var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, model);
var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, "Text");
var modelExplorer = containerExplorer.GetExplorerForExpression(propertyMetadata, modelAccessor());
var modelExpression = new ModelExpression(name: string.Empty, modelExplorer: modelExplorer);
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList(
Enumerable.Empty<TagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendHtml("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
})
{
TagMode = TagMode.SelfClosing,
};
output.PreContent.AppendHtml(expectedPreContent);
output.Content.AppendHtml(expectedContent);
output.PostContent.AppendHtml(originalPostContent);
var htmlGenerator = new TestableHtmlGenerator(metadataProvider)
{
ValidationAttributes =
{
{ "valid", "from validation attributes" },
}
};
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
viewContext.ViewData.TemplateInfo.HtmlFieldPrefix = nameAndId.Name;
var items = new SelectList(new[] { "", "outer text", "inner text", "other text" });
var savedDisabled = items.Select(item => item.Disabled).ToList();
var savedGroup = items.Select(item => item.Group).ToList();
var savedSelected = items.Select(item => item.Selected).ToList();
var savedText = items.Select(item => item.Text).ToList();
var savedValue = items.Select(item => item.Value).ToList();
var tagHelper = new SelectTagHelper(htmlGenerator)
{
For = modelExpression,
Items = items,
ViewContext = viewContext,
};
// Act
tagHelper.Init(tagHelperContext);
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(TagMode.SelfClosing, output.TagMode);
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, HtmlContentUtilities.HtmlContentToString(output.PostContent));
Assert.Equal(expectedTagName, output.TagName);
Assert.Single(
tagHelperContext.Items,
entry => (Type)entry.Key == typeof(SelectTagHelper));
Assert.Equal(savedDisabled, items.Select(item => item.Disabled));
Assert.Equal(savedGroup, items.Select(item => item.Group));
Assert.Equal(savedSelected, items.Select(item => item.Selected));
Assert.Equal(savedText, items.Select(item => item.Text));
Assert.Equal(savedValue, items.Select(item => item.Value));
//.........这里部分代码省略.........
示例7: IsEmptyOrWhiteSpace_FalseAfterAppendTagHelper
public void IsEmptyOrWhiteSpace_FalseAfterAppendTagHelper()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var copiedTagHelperContent = new DefaultTagHelperContent();
copiedTagHelperContent.SetContent("Hello");
// Act
tagHelperContent.AppendHtml(copiedTagHelperContent);
// Assert
Assert.False(tagHelperContent.IsEmptyOrWhiteSpace);
}
示例8: IsModified_TrueIfAppendedNull
public void IsModified_TrueIfAppendedNull()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
TagHelperContent NullContent = null;
// Act
tagHelperContent.AppendHtml(NullContent);
// Assert
Assert.True(tagHelperContent.IsModified);
}
示例9: GenerateAlertItem
/// <summary>
/// Generate the a alert dialog for a <see cref="OperationMessage" />.
/// </summary>
/// <param name="message">The <see cref="OperationMessage" /> instance.</param>
/// <param name="isClosable">If the alert dialog is closable.</param>
/// <param name="useTwoLineMode">If the two line mode should be used.</param>
/// <returns>The generated HTML content which represent as a HTML alert dialog.</returns>
private IHtmlContent GenerateAlertItem(OperationMessage message, bool isClosable, bool useTwoLineMode)
{
// Real style
var listStyle = isClosable ? MessageListStyle.AlertDialogClosable : MessageListStyle.AlertDialog;
var tag = new TagBuilder("div");
tag.AddCssClass("alert");
tag.AddCssClass(LevelClassMapper.MapLevel(message.Level, listStyle));
// Closable handling
if (isClosable)
{
tag.AddCssClass("alert-dismissible");
}
tag.MergeAttribute("role", "alert");
var content = new DefaultTagHelperContent();
if (isClosable)
{
// Close button
// TODO: Localization the "label" text
content.AppendHtml(
"<button type =\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">×</span></button>");
}
content.AppendHtml(GenerateMessageContent(message, useTwoLineMode));
// Internal content
tag.InnerHtml.AppendHtml(content);
return tag;
}
示例10: RenderScriptTags_FallbackSrc_WithFileVersion_EncodesAsExpected
public void RenderScriptTags_FallbackSrc_WithFileVersion_EncodesAsExpected()
{
// Arrange
var expectedContent =
"<script encoded='contains \"quotes\"' literal=\"HtmlEncode[[all HTML encoded]]\" " +
"mixed='HtmlEncode[[HTML encoded]] and contains \"quotes\"' " +
"src=\"HtmlEncode[[/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\"></script>" +
Environment.NewLine +
"<script>(isavailable()||document.write(\"JavaScriptEncode[[<script encoded=\'contains \"quotes\"\' " +
"literal=\"HtmlEncode[[all HTML encoded]]\" mixed=\'HtmlEncode[[HTML encoded]] and contains " +
"\"quotes\"' src=\"HtmlEncode[[fallback.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk]]\">" +
"</script>]]\"));</script>";
var mixed = new DefaultTagHelperContent();
mixed.Append("HTML encoded");
mixed.AppendHtml(" and contains \"quotes\"");
var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList
{
{ "asp-append-version", "true" },
{ "asp-fallback-src-include", "fallback.js" },
{ "asp-fallback-test", "isavailable()" },
{ new TagHelperAttribute("encoded", new HtmlString("contains \"quotes\""), HtmlAttributeValueStyle.SingleQuotes) },
{ "literal", "all HTML encoded" },
{ new TagHelperAttribute("mixed", mixed, HtmlAttributeValueStyle.SingleQuotes) },
{ "src", "/js/site.js" },
});
var output = MakeTagHelperOutput(
"script",
attributes: new TagHelperAttributeList
{
{ new TagHelperAttribute("encoded", new HtmlString("contains \"quotes\""), HtmlAttributeValueStyle.SingleQuotes) },
{ "literal", "all HTML encoded" },
{ new TagHelperAttribute("mixed", mixed, HtmlAttributeValueStyle.SingleQuotes) },
});
var hostingEnvironment = MakeHostingEnvironment();
var viewContext = MakeViewContext();
var helper = new ScriptTagHelper(
MakeHostingEnvironment(),
MakeCache(),
new HtmlTestEncoder(),
new JavaScriptTestEncoder(),
MakeUrlHelperFactory())
{
AppendVersion = true,
FallbackSrc = "fallback.js",
FallbackTestExpression = "isavailable()",
Src = "/js/site.js",
ViewContext = viewContext,
};
// Act
helper.Process(context, output);
// Assert
Assert.Equal("script", output.TagName);
Assert.Equal("/js/site.js?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", output.Attributes["src"].Value);
var content = HtmlContentUtilities.HtmlContentToString(output, new HtmlTestEncoder());
Assert.Equal(expectedContent, content);
}
示例11: IsEmptyOrWhiteSpace_FalseAfterAppendTagHelperContentTwice_WithCharByCharWriteTo
public void IsEmptyOrWhiteSpace_FalseAfterAppendTagHelperContentTwice_WithCharByCharWriteTo()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var copiedTagHelperContent = new CharByCharNonWhiteSpaceHtmlContent();
// Act
tagHelperContent.AppendHtml(copiedTagHelperContent);
tagHelperContent.AppendHtml(copiedTagHelperContent);
// Assert
Assert.False(tagHelperContent.IsEmptyOrWhiteSpace);
}
示例12: IsEmptyOrWhiteSpace_TrueAfterAppendTagHelperContentTwice_WithDataToEncode
public void IsEmptyOrWhiteSpace_TrueAfterAppendTagHelperContentTwice_WithDataToEncode(string data)
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var copiedTagHelperContent = new DefaultTagHelperContent();
copiedTagHelperContent.Append(data);
// Act
tagHelperContent.AppendHtml(copiedTagHelperContent);
tagHelperContent.AppendHtml(copiedTagHelperContent);
// Assert
Assert.True(tagHelperContent.IsEmptyOrWhiteSpace);
}
示例13: IsEmptyOrWhiteSpace_TrueAfterAppendHtmlTwice
public void IsEmptyOrWhiteSpace_TrueAfterAppendHtmlTwice(string data)
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
// Act
tagHelperContent.AppendHtml(data);
tagHelperContent.AppendHtml(data);
// Assert
Assert.True(tagHelperContent.IsEmptyOrWhiteSpace);
}
示例14: MoveTo_DoesDeepMove
public void MoveTo_DoesDeepMove()
{
// Arrange
var source = new DefaultTagHelperContent();
var nested = new DefaultTagHelperContent();
source.AppendHtml(nested);
nested.AppendHtml(new HtmlEncodedString("hello"));
source.Append("Test");
var items = new List<object>();
var destination = new HtmlContentBuilder(items);
destination.Append("some-content");
// Act
source.MoveTo(destination);
// Assert
Assert.Equal(string.Empty, source.GetContent());
Assert.Equal(string.Empty, nested.GetContent());
Assert.Equal(3, items.Count);
Assert.Equal("some-content", Assert.IsType<string>(items[0]));
Assert.Equal("hello", Assert.IsType<HtmlEncodedString>(items[1]).Value);
Assert.Equal("Test", Assert.IsType<string>(items[2]));
}
示例15: GenerateNormalList
/// <summary>
/// Generate the a list for a series of <see cref="OperationMessage" /> items.
/// </summary>
/// <param name="messages">The collection of all <see cref="OperationMessage" /> items.</param>
/// <param name="useTwoLineMode">If the two line mode should be used.</param>
/// <returns>The generated HTML content which represent as a HTML list.</returns>
private IHtmlContent GenerateNormalList(IEnumerable<OperationMessage> messages, bool useTwoLineMode)
{
var tag = new TagBuilder("ul");
tag.AddCssClass("list-group");
var content = new DefaultTagHelperContent();
foreach (var message in messages)
{
content.AppendHtml(GenerateNormalItem(message, useTwoLineMode));
}
tag.InnerHtml.AppendHtml(content);
return tag;
}