本文整理汇总了C#中Microsoft.AspNet.Razor.Runtime.TagHelpers.DefaultTagHelperContent类的典型用法代码示例。如果您正苦于以下问题:C# DefaultTagHelperContent类的具体用法?C# DefaultTagHelperContent怎么用?C# DefaultTagHelperContent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DefaultTagHelperContent类属于Microsoft.AspNet.Razor.Runtime.TagHelpers命名空间,在下文中一共展示了DefaultTagHelperContent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetChildContentAsync_CachesValue
public async Task GetChildContentAsync_CachesValue()
{
// Arrange
var defaultTagHelperContent = new DefaultTagHelperContent();
var expectedContent = string.Empty;
var executionContext = new TagHelperExecutionContext(
"p",
selfClosing: false,
items: null,
uniqueId: string.Empty,
executeChildContentAsync: () =>
{
if (string.IsNullOrEmpty(expectedContent))
{
expectedContent = "Hello from child content: " + Guid.NewGuid().ToString();
}
defaultTagHelperContent.SetContent(expectedContent);
return Task.FromResult(result: true);
},
startTagHelperWritingScope: () => { },
endTagHelperWritingScope: () => defaultTagHelperContent);
// Act
var content1 = await executionContext.GetChildContentAsync();
var content2 = await executionContext.GetChildContentAsync();
// Assert
Assert.Equal(expectedContent, content1.GetContent());
Assert.Equal(expectedContent, content2.GetContent());
}
示例2: CopyHtmlAttribute_DoesNotOverrideAttributes
public void CopyHtmlAttribute_DoesNotOverrideAttributes()
{
// Arrange
var attributeName = "hello";
var tagHelperOutput = new TagHelperOutput(
"p",
attributes: new Dictionary<string, object>()
{
{ attributeName, "world2" }
});
var expectedAttribute = new KeyValuePair<string, object>(attributeName, "world2");
var tagHelperContext = new TagHelperContext(
allAttributes: new Dictionary<string, object>(StringComparer.Ordinal)
{
{ attributeName, "world" }
},
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.Append("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
// Act
tagHelperOutput.CopyHtmlAttribute(attributeName, tagHelperContext);
// Assert
var attribute = Assert.Single(tagHelperOutput.Attributes);
Assert.Equal(expectedAttribute, attribute);
}
示例3: GetChildContentAsync_CachesValue
public async Task GetChildContentAsync_CachesValue()
{
// Arrange
var defaultTagHelperContent = new DefaultTagHelperContent();
var content = string.Empty;
var expectedContent = string.Empty;
var executionContext = new TagHelperExecutionContext(
"p",
tagMode: TagMode.StartTagAndEndTag,
items: new Dictionary<object, object>(),
uniqueId: string.Empty,
executeChildContentAsync: () =>
{
if (string.IsNullOrEmpty(expectedContent))
{
content = "Hello from child content: " + Guid.NewGuid().ToString();
expectedContent = $"HtmlEncode[[{content}]]";
}
defaultTagHelperContent.SetContent(content);
return Task.FromResult(result: true);
},
startTagHelperWritingScope: () => { },
endTagHelperWritingScope: () => defaultTagHelperContent);
// Act
var content1 = await executionContext.GetChildContentAsync(useCachedResult: true);
var content2 = await executionContext.GetChildContentAsync(useCachedResult: true);
// Assert
Assert.Equal(expectedContent, content1.GetContent(new CommonTestEncoder()));
Assert.Equal(expectedContent, content2.GetContent(new CommonTestEncoder()));
}
示例4: TagHelperOutput
/// <summary>
/// Instantiates a new instance of <see cref="TagHelperOutput"/>.
/// </summary>
/// <param name="tagName">The HTML element's tag name.</param>
/// <param name="attributes">The HTML attributes.</param>
public TagHelperOutput(
string tagName,
[NotNull] IDictionary<string, object> attributes)
{
TagName = tagName;
Attributes = new Dictionary<string, object>(attributes, StringComparer.OrdinalIgnoreCase);
_preContent = new DefaultTagHelperContent();
_content = new DefaultTagHelperContent();
_postContent = new DefaultTagHelperContent();
}
示例5: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput()
{
// Arrange
var expectedTagName = "not-div";
var metadataProvider = new TestModelMetadataProvider();
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
var validationSummaryTagHelper = new ValidationSummaryTagHelper(htmlGenerator)
{
ValidationSummary = ValidationSummary.All,
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var tagHelperContext = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList
{
{ "class", "form-control" }
});
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent("Custom Content");
Model model = null;
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
validationSummaryTagHelper.ViewContext = viewContext;
// Act
await validationSummaryTagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(2, output.Attributes.Count);
var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("class"));
Assert.Equal("form-control validation-summary-valid", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-summary"));
Assert.Equal("true", attribute.Value);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal("Custom Content<ul><li style=\"display:none\"></li>" + Environment.NewLine + "</ul>",
output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}
示例6: CanAppendContent
public void CanAppendContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "Hello World!";
// Act
tagHelperContent.Append(expected);
// Assert
Assert.Equal(expected, tagHelperContent.GetContent());
}
示例7: CanAppendContent
public void CanAppendContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "HtmlEncode[[Hello World!]]";
// Act
tagHelperContent.Append("Hello World!");
// Assert
Assert.Equal(expected, tagHelperContent.GetContent(new CommonTestEncoder()));
}
示例8: CanGetContent
public void CanGetContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "Hello World!";
tagHelperContent.SetContent(expected);
// Act
var actual = tagHelperContent.GetContent();
// Assert
Assert.Equal(expected, actual);
}
示例9: SetContent_WithTagHelperContent_WorksAsExpected
public void SetContent_WithTagHelperContent_WorksAsExpected(string expected)
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var copiedTagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(expected);
// Act
copiedTagHelperContent.SetContent(tagHelperContent);
// Assert
Assert.Equal(expected, copiedTagHelperContent.GetContent());
}
示例10: SetContentClearsExistingContent
public void SetContentClearsExistingContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "Hello World!";
tagHelperContent.SetContent("Contoso");
// Act
tagHelperContent.SetContent(expected);
// Assert
Assert.Equal(expected, tagHelperContent.GetContent());
}
示例11: SetContentClearsExistingContent
public void SetContentClearsExistingContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "HtmlEncode[[Hello World!]]";
tagHelperContent.SetContent("Contoso");
// Act
tagHelperContent.SetContent("Hello World!");
// Assert
Assert.Equal(expected, tagHelperContent.GetContent(new CommonTestEncoder()));
}
示例12: 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 context.GetChildContentAsync();
var footerContent = new DefaultTagHelperContent();
if (ShowDismiss)
{
footerContent.AppendFormat(@"<button type='button' class='btn btn-default' data-dismiss='modal'>{0}</button>", DismissText);
}
footerContent.Append(childContent);
var modalContext = (ModalContext)context.Items[typeof(ModalTagHelper)];
modalContext.Footer = footerContent;
output.SuppressOutput();
}
示例13: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput()
{
// Arrange
var expectedTagName = "not-form";
var metadataProvider = new TestModelMetadataProvider();
var tagHelperContext = new TagHelperContext(
allAttributes: new Dictionary<string, object>
{
{ "id", "myform" },
{ "asp-route-foo", "bar" },
{ "asp-action", "index" },
{ "asp-controller", "home" },
{ "method", "post" },
{ "asp-anti-forgery", true }
},
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
expectedTagName,
attributes: new Dictionary<string, object>
{
{ "id", "myform" },
{ "asp-route-foo", "bar" },
});
output.PostContent.SetContent("Something");
var urlHelper = new Mock<IUrlHelper>();
urlHelper
.Setup(mock => mock.Action(It.IsAny<UrlActionContext>())).Returns("home/index");
var htmlGenerator = new TestableHtmlGenerator(metadataProvider, urlHelper.Object);
var viewContext = TestableHtmlGenerator.GetViewContext(model: null,
htmlGenerator: htmlGenerator,
metadataProvider: metadataProvider);
var expectedPostContent = "Something" + htmlGenerator.GenerateAntiForgery(viewContext)
.ToString(TagRenderMode.SelfClosing);
var formTagHelper = new FormTagHelper
{
Action = "index",
AntiForgery = true,
Controller = "home",
Generator = htmlGenerator,
ViewContext = viewContext,
};
// Act
await formTagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(3, output.Attributes.Count);
var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("id"));
Assert.Equal("myform", attribute.Value);
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("method"));
Assert.Equal("post", attribute.Value);
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("action"));
Assert.Equal("home/index", attribute.Value);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.IsEmpty);
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}
示例14: ProcessAsync_CallsIntoGenerateValidationMessageWithExpectedParameters
public async Task ProcessAsync_CallsIntoGenerateValidationMessageWithExpectedParameters()
{
// Arrange
var expectedViewContext = CreateViewContext();
var generator = new Mock<IHtmlGenerator>();
generator
.Setup(mock =>
mock.GenerateValidationMessage(expectedViewContext, "Hello", null, null, null))
.Returns(new TagBuilder("span", new CommonTestEncoder()))
.Verifiable();
var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object)
{
For = CreateModelExpression("Hello")
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = "original post-content";
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
"span",
attributes: new TagHelperAttributeList());
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent);
validationMessageTagHelper.ViewContext = expectedViewContext;
// Act & Assert
await validationMessageTagHelper.ProcessAsync(context, output: output);
generator.Verify();
Assert.Equal("span", output.TagName);
Assert.Empty(output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
}
示例15: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput()
{
// Arrange
var expectedTagName = "not-span";
var metadataProvider = new TestModelMetadataProvider();
var modelExpression = CreateModelExpression("Name");
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
var validationMessageTagHelper = new ValidationMessageTagHelper(htmlGenerator)
{
For = modelExpression
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = "original post-content";
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList
{
{ "id", "myvalidationmessage" },
{ "for", modelExpression },
},
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList
{
{ "id", "myvalidationmessage" }
});
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent);
var viewContext = TestableHtmlGenerator.GetViewContext(model: null,
htmlGenerator: htmlGenerator,
metadataProvider: metadataProvider);
validationMessageTagHelper.ViewContext = viewContext;
// Act
await validationMessageTagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(4, output.Attributes.Count);
var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("id"));
Assert.Equal("myvalidationmessage", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("class"));
Assert.Equal("field-validation-valid", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-for"));
Assert.Equal("Name", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-replace"));
Assert.Equal("true", attribute.Value);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}