MvcHtmlString在ASP.NET Core 6 MVC中

huangapple go评论64阅读模式
英文:

MvcHtmlString in ASP.NET Core 6 MVC

问题

我已将它更新为新应用程序中的以下内容:

public static HtmlString BuildColoredIconTag(string iconName, string color)
{
    var icon = BuildInternalColoredIconTag(iconName, color);

    var htmlBuilder = new StringBuilder();

    htmlBuilder.Append(icon);

    var finalHtml = icon.ToHtmlString();

    return new HtmlString(finalHtml);
}

这是正确的修改,应该可以正常工作。在新应用程序中,MvcHtmlString 已经不再使用,而是使用 HtmlString。然后,您可以在.cshtml文件中继续使用@IconExtensions.BuildColoredIconTag(ViewData["icon"].ToString(), "text-info")。这个修改将解决您收到的空引用警告问题。

英文:

I am migrating an application from ASP.NET MVC 5 running on .NET 4.8 to ASP.NET Core 6 MVC.

The MVC 5 application has the following:

 public static MvcHtmlString BuildColoredIconTag(string iconName, string color)
 {
	var icon = BuildInternalColoredIconTag(iconName, color);

	var htmlBuilder = new StringBuilder();

	htmlBuilder.Append(icon);

	var finalHtml = icon.ToString(TagRenderMode.Normal);

	return MvcHtmlString.Create(finalHtml);
}

I updated it to the following in the new application:

public static HtmlString BuildColoredIconTag(string iconName, string color)
{
	var icon = BuildInternalColoredIconTag(iconName, color);

	var htmlBuilder = new StringBuilder();

	htmlBuilder.Append(icon);

	var finalHtml = icon.TagRenderMode.ToString();

	return new HtmlString(finalHtml);
}

Is this correct or should I update it to something else? I cannot test it yet because I am adding other components.

I have the following in a partial .cshtml file, but I receive null reference warnings:

@IconExtensions.BuildColoredIconTag(ViewData["icon"].ToString(), "text-info")

What should I update it to?

答案1

得分: 1

这是由你的csproj文件中的以下一行引起的:

<Nullable>enable</Nullable>

文档中所述:

> 默认情况下,禁用了可空注解和警告上下文。
> 这意味着你的现有代码编译时不需要更改,也不会生成新的警告。
> 从.NET 6开始,新项目在所有项目模板中包括<Nullable>enable</Nullable>元素。

如果你不喜欢它,只需禁用它:

&lt;Nullable&gt;disable&lt;/Nullable&gt;

如果你想保留它而不生成警告,可以将iconName声明为string?,或尝试使用??运算符,如下所示:

ViewData[&quot;icon&quot;]?.ToString()??&quot;DefaultIcon&quot;
英文:

It was caused by this line in your csproj file

&lt;Nullable&gt;enable&lt;/Nullable&gt;

as mentioned in the document:

> By default, nullable annotation and warning contexts are disabled.
> That means that your existing code compiles without changes and
> without generating any new warnings. Beginning with .NET 6, new
> projects include the <Nullable>enable</Nullable> element in all
> project templates.

If you don't like it ,just disable it

&lt;Nullable&gt;disable&lt;/Nullable&gt;

If you want to keep it without warning ,declearing iconName as string? or trying with ?? operater

like:

ViewData[&quot;icon&quot;]?.ToString()??&quot;DefaultIcon&quot;

答案2

得分: 0

如果您不想在VS中删除新的可为空内容,您可以像下面这样纯粹作为一个示例来做,我还会将其转换为一个扩展方法:

public static IHtmlString BuildColoredIconTag(this IHtmlHelper helper, string? iconName, string? color)
{
    if (string.IsNullOrEmpty(iconName)) return HtmlString.Empty; // 或者抛出异常

    if (string.IsNullOrEmpty(color)) return HtmlString.Empty; // 或者抛出异常

    var icon = BuildInternalColoredIconTag(iconName, color);

    var htmlBuilder = new StringBuilder();

    htmlBuilder.Append(icon);

    var finalHtml = icon.TagRenderMode.ToString();

    return new HtmlString(finalHtml);
}

然后可以像这样调用:

@Html.BuildColoredIconTag(ViewData["icon"].ToString(), "text-info")

为了进一步简化,您可以更改方法的签名:

public static IHtmlString BuildColoredIconTag(this IHtmlHelper helper, object? iconName, string? color)
{
    if (iconName == null) { return HtmlString.Empty; }

    // 其他代码...
}

然后可以这样调用:@Html.BuildColoredIconTag(ViewData["icon"], "text-info")

英文:

If you dont want to remove the new nullable stuff in VS, you could do something like this purely as an example, also I would convert it to an extension:

public static IHtmlString BuildColoredIconTag(this IHtmlHelper helper, string? iconName, string? color)
{
    if(string.IsNullOrEmpty(iconName)) return HtmlString.Empty;  // or throw

    if(string.IsNullOrEmpty(color)) return HtmlString.Empty; // or throw

    var icon = BuildInternalColoredIconTag(iconName, color);

    var htmlBuilder = new StringBuilder();

    htmlBuilder.Append(icon);

    var finalHtml = icon.TagRenderMode.ToString();

    return new HtmlString(finalHtml);
}

Then call like:

@Html.BuildColoredIconTag(ViewData[&quot;icon&quot;].ToString(), &quot;text-info&quot;)

And to simplify things even further you could change the method's signature to:

public static IHtmlString BuildColoredIconTag(this IHtmlHelper helper, object? iconName, string? color)
{
  if(iconName == null) {  return HtmlString.Empty; }
  
  ...

And then call with @Html.BuildColoredIconTag(ViewData[&quot;icon&quot;], &quot;text-info&quot;)

huangapple
  • 本文由 发表于 2023年4月7日 01:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定