asp.net mvc。将模型中的字符串数据插入到javascript中。最佳实践

时间:2022-09-18 08:15:23

I have to populate javascript code in html layout (hidden fields, method params) with string data from model.

我必须使用模型中的字符串数据填充html布局(隐藏字段,方法参数)中的javascript代码。

Html.Encode is not appropriate for my task because it encodes ' symbol, bypass : (that ruines object attributes declaration) and so on.

Html.Encode不适合我的任务,因为它编码'符号,绕过:(破坏对象属性声明)等等。

I wrote static helper class that is used from View like this:

我编写了静态帮助器类,从View中使用如下:

  alert('<%=ViewHelper.MakeJavaScriptSafe(Model.Message)%>');

I hope there is asp.net in-built function I don't know about for this task. Does it exist really?

我希望有asp.net内置函数我不知道这个任务。真的存在吗?

Thank you in advance.

先谢谢你。

1 个解决方案

#1


1  

I believe what you are looking for is an Anti-Cross Site Scripting library, like this one.

我相信你要找的是一个反跨站点脚本库,就像这个。

#1


1  

I believe what you are looking for is an Anti-Cross Site Scripting library, like this one.

我相信你要找的是一个反跨站点脚本库,就像这个。