Skip to content

How to fix this warning within a Loop? #34273

@Grandizer

Description

@Grandizer

Description

I have a method where I am creating a Fragment and I may or may not have any number of extra attributes. I need those to be part of the Fragment and I know there needs to be a sequence number. I have code below and no way I work about it can I get it to work. So I cannot get this ASP0006 warning to go away.

	protected override void OnParametersSet()
	{
		base.OnParametersSet();

		var iconData = Constants.Icons.FirstOrDefault(x => x.Name == Name);
		var totalItems = 0;

		if (iconData is null)
			return;

		Fragment = builder =>
		{
			builder.OpenElement(0, "svg");

			builder.AddAttribute(1, "xmlns", "http://www.w3.org/2000/svg");
			builder.AddAttribute(2, "viewBox", iconData.ViewBox);
			builder.AddAttribute(3, "preserveAspectRatio", "xMidYMid meet");

			if (Attributes != null)
			{
				var keys = Attributes.Keys.ToList();
				totalItems = keys.Count + 4;

				for (int i = 0; i < keys.Count; i++)
				{
					var key = keys[i];
					var value = Attributes[key];

					builder.AddAttribute(4 + i, key, value);
				}
			}

			if (!string.IsNullOrWhiteSpace(Title))
			{
				builder.OpenElement(4 + totalItems, "title");
				builder.AddContent(5 + totalItems, Title);
				builder.CloseElement();
			}

			builder.AddContent(6 + totalItems, new MarkupString(iconData.Path));
			builder.CloseElement();
		};
	}

Page URL

https://learn.microsoft.com/en-us/aspnet/core/diagnostics/asp0006?view=aspnetcore-9.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/diagnostics/asp0006.md

Document ID

d1bbdc46-2e5c-7dad-76cd-d363d0b19520

Article author

@safia

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions