本文整理汇总了C#中Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent.SetContent方法的典型用法代码示例。如果您正苦于以下问题:C# DefaultTagHelperContent.SetContent方法的具体用法?C# DefaultTagHelperContent.SetContent怎么用?C# DefaultTagHelperContent.SetContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent
的用法示例。
在下文中一共展示了DefaultTagHelperContent.SetContent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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 HtmlTestEncoder()));
}
示例2: 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());
}
示例3: 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);
}
示例4: 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());
}
示例5: 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;
}
示例6: 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);
}
示例7: Fluent_SetContent_AppendFormat_Append_WritesExpectedContent
public void Fluent_SetContent_AppendFormat_Append_WritesExpectedContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "HtmlEncode[[First ]]HtmlEncode[[Second]] Third HtmlEncode[[Fourth]]";
// Act
tagHelperContent
.SetContent("First ")
.AppendFormat("{0} Third ", "Second")
.Append("Fourth");
// Assert
Assert.Equal(expected, tagHelperContent.GetContent(new HtmlTestEncoder()));
}
示例8: CanClearContent
public void CanClearContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Hello World");
// Act
tagHelperContent.Clear();
// Assert
Assert.True(tagHelperContent.IsEmptyOrWhiteSpace);
}
示例9: MakeTagHelperOutput
private TagHelperOutput MakeTagHelperOutput(
string tagName,
TagHelperAttributeList attributes = null,
string childContent = null)
{
attributes = attributes ?? new TagHelperAttributeList();
return new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(childContent);
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
}
示例10: 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());
}
示例11: 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();
}
示例12: SetHtmlContent_WithTagHelperContent_WorksAsExpected
public void SetHtmlContent_WithTagHelperContent_WorksAsExpected(string content, string expected)
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var copiedTagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(content);
// Act
copiedTagHelperContent.SetHtmlContent(tagHelperContent);
// Assert
Assert.Equal(expected, copiedTagHelperContent.GetContent(new HtmlTestEncoder()));
}
示例13: CanIdentifyEmptyOrWhiteSpace
public void CanIdentifyEmptyOrWhiteSpace(string data)
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
// Act
tagHelperContent.SetContent(" ");
tagHelperContent.Append(data);
// Assert
Assert.True(tagHelperContent.IsEmptyOrWhiteSpace);
}
示例14: IsModified_TrueAfterSetContent
public void IsModified_TrueAfterSetContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
// Act
tagHelperContent.SetContent(string.Empty);
// Assert
Assert.True(tagHelperContent.IsModified);
}
示例15: CanAppendDefaultTagHelperContent
public void CanAppendDefaultTagHelperContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var helloWorldContent = new DefaultTagHelperContent();
helloWorldContent.SetContent("HelloWorld");
var expected = "Content was HtmlEncode[[HelloWorld]]";
// Act
tagHelperContent.AppendFormat("Content was {0}", helloWorldContent);
// Assert
Assert.Equal(expected, tagHelperContent.GetContent(new HtmlTestEncoder()));
}