简体   繁体   English

"ASP.NET MVC 将原始 HTML 从控制器传递到视图"

[英]ASP.NET MVC Passing Raw HTML from Controller to View

I have been scratching my head about this for a few days, and I am not sure if it is an issue with my environment or the code itself basing this on being to ASP.NET MVC (although I have 5 years experience in C#).这几天我一直在摸不着头脑,我不确定这是我的环境问题还是基于 ASP.NET MVC 的代码本身的问题(尽管我有 5 年的 C# 经验)。 I am using a recent clean install of Win7x64 and VS 2008 with all the patches.我正在使用带有所有补丁的 Win7x64 和 VS 2008 最近的全新安装。

I have raw HTML stored in a database table that is selectively loaded by the controller based on a few rules which I do not have control over.我将原始 HTML 存储在数据库表中,控制器根据我无法控制的一些规则选择性地加载该表。 Unfortunately when attempt to stuff the value into a view data in the control like such:不幸的是,当尝试将值填充到控件中的视图数据中时,如下所示:

ViewData["HTMLData"] = DAO.HTMLDataGet();

When I see the output, it is escaped/HTML Encoded.当我看到输出时,它是转义/HTML 编码的。 I tried using the following all of which did not seem to resolve this issue:我尝试使用以下所有这些似乎都无法解决此问题:

<%: HttpUtility.HtmlDecode(ViewData["HTMLData"].ToString())%>

And...和...

<%: Server.HtmlDecode(ViewData["HTMLData"].ToString())%>

And...和...

<%: Html.Raw(ViewData["HTMLData"].ToString())%>

...it grabs the raw HTML from the database table just fine, however it keeps forcing that blasted encoding regardless of what I try. ...它可以很好地从数据库表中获取原始 HTML,但是无论我尝试什么,它都会继续强制执行该爆破编码。 From what I read on the MSDN, there was a foot note about problems resulting from HTML not being decoded properly that contained spaces (which mine does).从我在 MSDN 上阅读的内容来看,有一个脚注是关于由于 HTML 未正确解码而导致的问题,其中包含空格(我的就是这样)。 Since I doubt I am the only one who has faced this I am turning to you folks for some ideas.因为我怀疑我是唯一一个面临这个问题的人,所以我向你们寻求一些想法。

I am about to Kludge my way though it with a regex in the view to do page cleanup, but thought it would be better to get some advice from some other folks first before I brute force it.Thanks in advance.我打算用正则表达式来进行页面清理,但我认为最好先从其他人那里得到一些建议,然后再强行使用它。提前致谢。

<%: means "encode if necessary". <%: 表示 “必要时编码”。 If you don't want that, then the lazy approach would be to use <%= , but frankly I suggest you instead wrap it in IHtmlString , for example: 如果你不想那样,那么懒惰的方法就是使用<%= ,但坦率地说,我建议你把它包装在IHtmlString ,例如:

string yourEncodedHtml = ...
var html = new MvcHtmlString(yourEncodedHtml);

Now, if you store that and show it, it should take the html "as is". 现在,如果你存储并显示它,它应该采取的HTML“原样”。

Try using: <%= %> 尝试使用: <%=%>

<%= Html.Raw(ViewData["HTMLData"].ToString())%>

<%: %> is Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC) <%:%>是ASP.NET 4(和ASP.NET MVC)中HTML编码输出的语法

For More Details 更多细节

How to HTML Encode Content Today 如何HTML编码今天的内容

ASP.NET applications (especially those using ASP.NET MVC) often rely on using <%= %> code-nugget expressions to render output. ASP.NET应用程序(尤其是那些使用ASP.NET MVC的应用程序)通常依赖于使用<%=%> code-nugget表达式来呈现输出。 Developers today often use the Server.HtmlEncode() or HttpUtility.Encode() helper methods within these expressions to HTML encode the output before it is rendered. 今天的开发人员经常在这些表达式中使用Server.HtmlEncode()或HttpUtility.Encode()辅助方法,在输出呈现之前对输出进行HTML编码。

While this works fine, there are two downsides of it: 虽然这很好,但它有两个缺点:

It is a little verbose Developers often forget to call the Server.HtmlEncode method – and there is no easy way to verify its usage across an app 这是一个有点冗长的开发人员经常忘记调用Server.HtmlEncode方法 - 并没有简单的方法来验证它在应用程序中的使用

New <%: %> Code Nugget Syntax 新的<%:%>代码块语法

With ASP.NET 4 we are introducing a new code expression syntax ( <%: %> ) that renders output like <%= %> blocks do – but which also automatically HTML encodes it before doing so. 在ASP.NET 4中,我们引入了一种新的代码表达式语法( <%:%> ),它可以像<%=%>块一样呈现输出 - 但在执行此操作之前,它还会自动对HTML进行编码。 This eliminates the need to explicitly HTML encode content. 这消除了对显式HTML编码内容的需要。

We chose the <%: %> syntax so that it would be easy to quickly replace existing instances of <%= %> code blocks. 我们选择<%:%>语法,以便快速替换<%=%>代码块的现有实例。 It also enables you to easily search your code-base for <%= %> elements to find and verify any cases where you are not using HTML encoding within your application to ensure that you have the correct behavior. 它还使您能够轻松搜索代码库中的<%=%>元素,以查找并验证您在应用程序中未使用HTML编码的任何情况,以确保您具有正确的行为。

You almost had it with the things you tried.你几乎用过你尝试过的东西。 Gave me the idea to do this, which works:给了我这样做的想法,这很有效:

Html.Raw(HttpUtility.HtmlDecode(ViewData["HTMLData"].ToString()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM