cshtml/vbhtml 和新的 @ 包含服务器端数据,但没有编码?

发布于 2024-12-25 00:36:02 字数 1130 浏览 0 评论 0 原文

我开始学习 MVC,并想同时使用 KnockOut.js

我无法理解为什么新的 @ include 标签将所有内容编码为 HTML(编辑:好吧,我现在知道如何防止 XSS)..但更糟糕的是如何阻止它这样做..

我有这个代码。

@{
    ViewBag.Title = "Home Page";
    var myData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);
}

<script type="text/javascript">
    var initialData =  @myData ;

哪个产生这个源

<script type="text/javascript">

var initialData =  [{&quot;Title&quot;:&quot;Tall Hat&quot;,&quot;Price&quot;:49.95},{&quot;Title&quot;:&quot;Long Cloak&quot;,&quot;Price&quot;:78.25}] ;

确定。所以尝试使用HttpUtility.HtmlDecode..并且JavaScript中包含的位没有任何反应,因为剃刀引擎正在重新编码它?但如果它使用编码,则重新编码 html..briallinat。

我不知道如何停止编码。

msdn说使用@:但这在javascript块中不起作用,我什至尝试过 @{ @:new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);}

这只是一些奇怪的事情并导致其他错误。

在MVC模型中应该如何实现呢?

在 aspx 中使用

var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>;

工作正常..

I started to learn MVC and in conjunction want to use the KnockOut.js

I am having trouble understanding why the new @ include tag encodes everything to HTML(edit: ok i know now to prevent XSS).. but even worse how to stop it from doing that..

I have this code.

@{
    ViewBag.Title = "Home Page";
    var myData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);
}

<script type="text/javascript">
    var initialData =  @myData ;

Which produces this source

<script type="text/javascript">

var initialData =  [{"Title":"Tall Hat","Price":49.95},{"Title":"Long Cloak","Price":78.25}] ;

Ok. so tried using HttpUtility.HtmlDecode.. and nothing happens on the included bit in the javascript because the razor engine is reencoding it? but if it use the encode then reecnodes the encodes html.. briallinat.

I cannot work out how to stop the encoding.

The msdn says use @: but that does not work in the javascript block, i even tried
@{ @:new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);}

That just something wierd and causes other errors.

How should this be done in the MVC model?

In aspx using

var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>;

works fine..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2025-01-01 00:36:02

Html.Raw(string) 将以原始、未编码的形式写出字符串。前提是您的字符串一开始就没有编码。

根据要求的代码示例(?!?!)

@{
    ViewBag.Title = "Home Page";
    var myData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);
}

<script type="text/javascript">
    var initialData =  @Html.Raw(myData) ;

Html.Raw(string) will write out your string in its raw, unencoded form. Provided your string is not encoded to begin with.

code example as requested (?!?!)

@{
    ViewBag.Title = "Home Page";
    var myData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);
}

<script type="text/javascript">
    var initialData =  @Html.Raw(myData) ;
不交电费瞎发啥光 2025-01-01 00:36:02

MVC(或本例中的 Razor 视图引擎)使用 MvcHtmlString。要解决这个问题,请使用:

<script type="text/javascript">
    var initialData =  "@MvcHtmlString.Create(myData)";
</script>

然后假设您的字符串已经被编码。

MVC (or the Razor view engine in this case) encodes all your strings as a HTML string using the MvcHtmlString by default. To get around it, use:

<script type="text/javascript">
    var initialData =  "@MvcHtmlString.Create(myData)";
</script>

This then assumes your string has already been encoded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文