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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10331019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:54:18  来源:igfitidea点击:

Difference between MvcHtmlString.Create() and Html.Raw()

asp.net-mvcrazorasp.net-mvc-4

提问by Julian

I am creating a MVC-Project. Using MVC 4 and Razor. After building some pages I was wondering: what is the difference between

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

MvcHtmlString.Create()

and

Html.Raw()

Would be nice if you could help me here to understand that.

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

Thanks in advance!

提前致谢!

采纳答案by Erik van Brakel

This is an excellent opportunity to look at the source code that's available to us for ASP.NET (https://github.com/aspnet/AspNetWebStack/).

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

Looking at HtmlHelper.cs, this is the code for Html.Raw():

查看 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());
}

And this is the code for the MvcHtmlString class:

这是 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);
        }
    }
}

The most significant difference is that Html.Raw()accepts any object, while MvcHtmlString.Create()only accepts strings. Also, Html.Raw()returns an interface, while the Create method returns an MvcHtmlString object. Lastly, the Create deals with null differently.

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

回答by Guffa

There is no practical difference.

没有实际区别。

The MvcHtmlString.Createcreates an instance of MvcHtmlString, while the Html.Rawmethod creates an instance of HtmlString, but MvcHtmlStringjust inherits from HtmlString, so they work the same.

MvcHtmlString.Create创建的实例MvcHtmlString,而Html.Raw方法创建的实例HtmlString,但MvcHtmlString只从继承HtmlString,所以他们的工作是相同的。