Escaping JavaScript string literals in views

前端 未结 4 2152
栀梦
栀梦 2020-12-15 16:20

Is there a utility function for escaping JavaScript in ASP.NET MVC views? I often need to init a little snippet of JavaScript using some values from the view; for instance I

4条回答
  •  借酒劲吻你
    2020-12-15 16:38

    In .NET 4, The HttpUtility class has a variety of static encoding methods for various contexts, including a JavaScriptStringEncode method for this particular purpose.

    It's often simpler to just use JSON deserialization, though.

提交回复
热议问题