MvcHtmlString.Create()和Html.Raw之间差() [英] Difference between MvcHtmlString.Create() and Html.Raw()

查看:723
本文介绍了MvcHtmlString.Create()和Html.Raw之间差()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个MVC-项目。使用MVC 4和剃刀。建设一些网页后,我在想:什么是

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

MvcHtmlString.Create()

Html.Raw()

将是很好,如果你能帮助我在这里要理解这一点。

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

在此先感谢!

推荐答案

这是看源$ C ​​$ C(这是提供给我们用于ASP.NET的 HTTP://aspnetwebstack.$c$cplex.com

This is an excellent opportunity to look at the source code that's available to us for ASP.NET (http://aspnetwebstack.codeplex.com).

看着HtmlHelper.cs,这是code为 Html.Raw()

Looking at HtmlHelper.cs, this is the code for 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类code:

And this is the code for the MvcHtmlString class:

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()返回一个接口,而创建方法返回一个MvcHtmlString对象。
最后,用null创建不同的交易。

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.

这篇关于MvcHtmlString.Create()和Html.Raw之间差()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆