本文整理汇总了C#中Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent类的典型用法代码示例。如果您正苦于以下问题:C# DefaultTagHelperContent类的具体用法?C# DefaultTagHelperContent怎么用?C# DefaultTagHelperContent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DefaultTagHelperContent类属于Microsoft.AspNetCore.Razor.TagHelpers命名空间,在下文中一共展示了DefaultTagHelperContent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetOutputContentAsync_SetsOutputsContent
public async Task SetOutputContentAsync_SetsOutputsContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var content = "Hello from child content";
var executionContext = new TagHelperExecutionContext(
"p",
tagMode: TagMode.StartTagAndEndTag,
items: new Dictionary<object, object>(),
uniqueId: string.Empty,
executeChildContentAsync: () =>
{
tagHelperContent.SetContent(content);
return Task.FromResult(result: true);
},
startTagHelperWritingScope: _ => { },
endTagHelperWritingScope: () => tagHelperContent);
// Act
await executionContext.SetOutputContentAsync();
// Assert
Assert.Equal(content, executionContext.Output.Content.GetContent());
}
示例2: GetChildContentAsync_CallsGetChildContentAsync
public async Task GetChildContentAsync_CallsGetChildContentAsync()
{
// Arrange
bool? passedUseCacheResult = null;
HtmlEncoder passedEncoder = null;
var content = new DefaultTagHelperContent();
var output = new TagHelperOutput(
tagName: "tag",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult, encoder) =>
{
passedUseCacheResult = useCachedResult;
passedEncoder = encoder;
return Task.FromResult<TagHelperContent>(content);
});
// Act
var result = await output.GetChildContentAsync();
// Assert
Assert.True(passedUseCacheResult.HasValue);
Assert.True(passedUseCacheResult.Value);
Assert.Null(passedEncoder);
Assert.Same(content, result);
}
示例3: GenerateGroupsAndOptions
public static IHtmlContent GenerateGroupsAndOptions(string optionLabel, IEnumerable<SelectListItem> selectList)
{
var listItemBuilder = new DefaultTagHelperContent();
// Make optionLabel the first item that gets rendered.
if (optionLabel != null)
{
listItemBuilder.AppendLine(GenerateOption(new SelectListItem
{
Text = optionLabel,
Value = string.Empty,
Selected = false
}));
}
// Group items in the SelectList if requested.
// Treat each item with Group == null as a member of a unique group
// so they are added according to the original order.
var groupedSelectList = selectList.GroupBy(item => item.Group?.GetHashCode() ?? item.GetHashCode());
foreach (var group in groupedSelectList)
{
var optGroup = group.First().Group;
if (optGroup != null)
{
var groupBuilder = new TagBuilder("optgroup");
if (optGroup.Name != null)
{
groupBuilder.MergeAttribute("label", optGroup.Name);
}
if (optGroup.Disabled)
{
groupBuilder.MergeAttribute("disabled", "disabled");
}
groupBuilder.InnerHtml.AppendLine();
foreach (var item in group)
{
groupBuilder.InnerHtml.AppendLine(GenerateOption(item));
}
listItemBuilder.AppendLine(groupBuilder);
}
else
{
foreach (var item in group)
{
listItemBuilder.AppendLine(GenerateOption(item));
}
}
}
return listItemBuilder;
}
示例4: SetHtmlContent_TextIsNotFurtherEncoded
public void SetHtmlContent_TextIsNotFurtherEncoded()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
// Act
tagHelperContent.SetHtmlContent("Hi");
// Assert
Assert.Equal("Hi", tagHelperContent.GetContent(new HtmlTestEncoder()));
}
示例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 TagHelperAttributeList(
Enumerable.Empty<TagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList
{
{ "class", "form-control" }
},
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
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: CanSetContent
public void CanSetContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "HtmlEncode[[Hello World!]]";
// Act
tagHelperContent.SetContent("Hello World!");
// Assert
Assert.Equal(expected, tagHelperContent.GetContent(new HtmlTestEncoder()));
}
示例7: 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()));
}
示例8: Reset_ClearsTheExpectedFields
public void Reset_ClearsTheExpectedFields()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("hello world");
// Act
tagHelperContent.Reinitialize();
Assert.False(tagHelperContent.IsModified);
Assert.Equal(string.Empty, tagHelperContent.GetContent());
}
示例9: GetSimpleTagHelperOutput
protected TagHelperOutput GetSimpleTagHelperOutput()
{
var output = new TagHelperOutput("a", new TagHelperAttributeList(), (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
output.Content.SetContent(string.Empty);
return output;
}
示例10: Reinitialize_AllowsOutputToBeReused
public async Task Reinitialize_AllowsOutputToBeReused()
{
// Arrange
var initialOutputChildContent = new DefaultTagHelperContent();
initialOutputChildContent.SetContent("Initial output content.");
var expectedGetChildContentContent = "Initial get child content content";
var initialGetChildContent = new DefaultTagHelperContent();
initialGetChildContent.SetContent(expectedGetChildContentContent);
Func<bool, HtmlEncoder, Task<TagHelperContent>> initialGetChildContentAsync =
(useCachedResult, encoder) => Task.FromResult<TagHelperContent>(initialGetChildContent);
var initialTagMode = TagMode.StartTagOnly;
var initialAttributes = new TagHelperAttributeList
{
{ "name", "value" }
};
var initialTagName = "initialTagName";
var output = new TagHelperOutput(initialTagName, initialAttributes, initialGetChildContentAsync)
{
TagMode = initialTagMode,
Content = initialOutputChildContent,
};
output.PreContent.SetContent("something");
output.PostContent.SetContent("something");
output.PreElement.SetContent("something");
output.PostElement.SetContent("something");
var expectedTagName = "newTagName";
var expectedTagMode = TagMode.SelfClosing;
// Act
output.Reinitialize(expectedTagName, expectedTagMode);
// Assert
Assert.Equal(expectedTagName, output.TagName);
Assert.Equal(expectedTagMode, output.TagMode);
Assert.Empty(output.Attributes);
var getChildContent = await output.GetChildContentAsync();
var content = getChildContent.GetContent();
// We're expecting the initial child content here because normally the TagHelper infrastructure would
// swap out the inner workings of GetChildContentAsync to work with its reinitialized state.
Assert.Equal(expectedGetChildContentContent, content);
Assert.False(output.PreContent.IsModified);
Assert.False(output.PostContent.IsModified);
Assert.False(output.PreElement.IsModified);
Assert.False(output.PostElement.IsModified);
}
示例11: 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();
}
示例12: ProcessAsync_CallsIntoGenerateRouteFormWithExpectedParameters
public async Task ProcessAsync_CallsIntoGenerateRouteFormWithExpectedParameters()
{
// Arrange
var viewContext = CreateViewContext();
var context = new TagHelperContext(
allAttributes: new TagHelperAttributeList(
Enumerable.Empty<TagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator
.Setup(mock => mock.GenerateRouteForm(
viewContext,
"Default",
It.Is<Dictionary<string, object>>(m => string.Equals(m["name"], "value")),
null,
null))
.Returns(new TagBuilder("form"))
.Verifiable();
var formTagHelper = new FormTagHelper(generator.Object)
{
Antiforgery = false,
Route = "Default",
ViewContext = viewContext,
RouteValues =
{
{ "name", "value" },
},
};
// Act & Assert
await formTagHelper.ProcessAsync(context, output);
generator.Verify();
Assert.Equal("form", output.TagName);
Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode);
Assert.Empty(output.Attributes);
Assert.Empty(output.PreElement.GetContent());
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.GetContent().Length == 0);
Assert.Empty(output.PostContent.GetContent());
Assert.Empty(output.PostElement.GetContent());
}
示例13: ProcessAsync_AspAreaOverridesAspRouteArea
public async Task ProcessAsync_AspAreaOverridesAspRouteArea()
{
// Arrange
var viewContext = CreateViewContext();
var context = new TagHelperContext(
allAttributes: new TagHelperAttributeList(
Enumerable.Empty<TagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var expectedRouteValues = new Dictionary<string, object> { { "area", "Admin" } };
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator
.Setup(mock => mock.GenerateForm(
viewContext,
"Index",
"Home",
expectedRouteValues,
null,
null))
.Returns(new TagBuilder("form"))
.Verifiable();
var formTagHelper = new FormTagHelper(generator.Object)
{
Action = "Index",
Antiforgery = false,
Controller = "Home",
Area = "Admin",
RouteValues = new Dictionary<string, string> { { "area", "Client" } },
ViewContext = viewContext,
};
// Act
await formTagHelper.ProcessAsync(context, output);
// Assert
generator.Verify();
Assert.Equal("form", output.TagName);
Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode);
Assert.Empty(output.Attributes);
Assert.Empty(output.PreElement.GetContent());
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.GetContent().Length == 0);
Assert.Empty(output.PostContent.GetContent());
}
示例14: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput()
{
// Arrange
var expectedTagName = "not-form";
var metadataProvider = new TestModelMetadataProvider();
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList
{
{ "id", "myform" },
{ "asp-route-name", "value" },
{ "asp-action", "index" },
{ "asp-controller", "home" },
{ "method", "post" },
{ "asp-antiforgery", true }
},
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList
{
{ "id", "myform" },
},
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
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" +
HtmlContentUtilities.HtmlContentToString(
htmlGenerator.GenerateAntiforgery(viewContext),
HtmlEncoder.Default);
var formTagHelper = new FormTagHelper(htmlGenerator)
{
Action = "index",
Antiforgery = true,
Controller = "home",
ViewContext = viewContext,
RouteValues =
{
{ "name", "value" },
},
};
// Act
await formTagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(3, output.Attributes.Count);
var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("id"));
Assert.Equal("myform", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("method"));
Assert.Equal("post", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("action"));
Assert.Equal("home/index", attribute.Value);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.GetContent().Length == 0);
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}
示例15: ProcessAsync_BindsRouteValues
public async Task ProcessAsync_BindsRouteValues()
{
// Arrange
var testViewContext = CreateViewContext();
var context = new TagHelperContext(
allAttributes: new TagHelperAttributeList(
Enumerable.Empty<TagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var expectedAttribute = new TagHelperAttribute("asp-ROUTEE-NotRoute", "something");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
output.Attributes.Add(expectedAttribute);
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator
.Setup(mock => mock.GenerateForm(
It.IsAny<ViewContext>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<object>(),
It.IsAny<string>(),
It.IsAny<object>()))
.Callback<ViewContext, string, string, object, string, object>(
(viewContext, actionName, controllerName, routeValues, method, htmlAttributes) =>
{
// Fixes Roslyn bug with lambdas
generator.ToString();
var routeValueDictionary = (Dictionary<string, object>)routeValues;
Assert.Equal(2, routeValueDictionary.Count);
var routeValue = Assert.Single(routeValueDictionary, attr => attr.Key.Equals("val"));
Assert.Equal("hello", routeValue.Value);
routeValue = Assert.Single(routeValueDictionary, attr => attr.Key.Equals("-Name"));
Assert.Equal("Value", routeValue.Value);
})
.Returns(new TagBuilder("form"))
.Verifiable();
var formTagHelper = new FormTagHelper(generator.Object)
{
Action = "Index",
Antiforgery = false,
ViewContext = testViewContext,
RouteValues =
{
{ "val", "hello" },
{ "-Name", "Value" },
},
};
// Act & Assert
await formTagHelper.ProcessAsync(context, output);
Assert.Equal("form", output.TagName);
Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode);
var attribute = Assert.Single(output.Attributes);
Assert.Equal(expectedAttribute, attribute);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.GetContent().Length == 0);
Assert.Empty(output.PostContent.GetContent());
generator.Verify();
}