gpt4 book ai didi

asp.net-mvc - MvcHtmlString.Create() 和 Html.Raw() 之间的区别

转载 作者:行者123 更新时间:2023-12-03 01:01:00 26 4
gpt4 key购买 nike

我正在创建一个 MVC 项目。使用 MVC 4 和 Razor。构建了一些页面后,我想知道:之间有什么区别

MvcHtmlString.Create()

Html.Raw()

如果您能帮助我理解这一点,那就太好了。

提前致谢!

最佳答案

这是查看我们可用的 ASP.NET ( https://github.com/aspnet/AspNetWebStack/ ) 源代码的绝佳机会。

查看 HtmlHelper.cs,这是 Html.Raw() 的代码:

public IHtmlString Raw(string value)
{
return new HtmlString(value);
}

public IHtmlString Raw(object value)
{
return new HtmlString(value == null ? null : value.ToString());
}

这是 MvcHtmlString 类的代码:

namespace System.Web.Mvc
{
public sealed class MvcHtmlString : HtmlString
{
[SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "MvcHtmlString is immutable")]
public static readonly MvcHtmlString Empty = Create(String.Empty);

private readonly string _value;

public MvcHtmlString(string value)
: base(value ?? String.Empty)
{
_value = value ?? String.Empty;
}

public static MvcHtmlString Create(string value)
{
return new MvcHtmlString(value);
}

public static bool IsNullOrEmpty(MvcHtmlString value)
{
return (value == null || value._value.Length == 0);
}
}
}

最显着的区别是 Html.Raw() 接受任何对象,而 MvcHtmlString.Create() 仅接受字符串。此外,Html.Raw() 返回一个接口(interface),而 Create 方法返回一个 MvcHtmlString 对象。最后,Create 对 null 的处理方式有所不同。

关于asp.net-mvc - MvcHtmlString.Create() 和 Html.Raw() 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10331019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com