Encoding and decoding data the way MVC likes it

孤街浪徒 提交于 2019-12-24 14:49:28

问题


I get data from MVC and encode it using HttpUtility.HtmlEncode. The problem is that MVC is not fond of form values containing the ampersand (&) symbol. For example, this...

HttpUtility.HtmlEncode("'");

...results in '. I fetch it on a GET, but if I submit this on a POST, I see the following action error:

Exception = {"A potentially dangerous Request.Form value was detected from the client (selection=\"'...\")."}

How can I encode my data on a GET in a way that makes MVC happy on a POST? I cannot set the property to [AllowHtml] because the model is within a separate class in a 3rd party DLL that I do not have control over, nor can I re-implement it, it seems.


回答1:


Instead of dealing with Encoding and Decoding Urls just post the Base64String




回答2:


I found a better way of serializing/deserializing and I wanted to share it with everyone. Since I am using JavaScript and DataTables.NET to do AJAX POST requests, I was able to use the System.Web.Helpers methods called Json.Encode and Json.Decode, since they convert directly to JSON (JavaScript Object Notation) and back, it seems to work perfectly from JavaScript and up to MVC for my needs, and I should have used it all along.




回答3:


How about using UrlEncode instead? https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode(v=vs.110).aspx



来源:https://stackoverflow.com/questions/35654802/encoding-and-decoding-data-the-way-mvc-likes-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!