开发者

c# mvc escape json

I use MVC controller to return json:

 public J开发者_开发问答sonResult Json()
        {
            return Json(MyJsonString);
        }

I want to Json to be escaped. Does anyone know exact method to accomplish this? Thanks

EDIT1: json is being sent for flash video player

--MB


I am taking a stab at this based on your sample and comments, but is MyJsonString a string that is already JSON encoded? It sounds like it is, and therefor it is being double encoded.

If so, then you may have better luck returning the object that you used to create the JSON encoded string, like:

public virtual ActionResult Json()
    {
        var someObject = MethodThatCreatesAnObject();
        //or just create it on the fly
        //var someObject = new { val1 = "value", val2 = "another" };
        return Json(someObject);
    }

Alternatively, you could create a control that has string as its model type and that doesn't have any content in it except <%: Model %> and then you could do something like:

public virtual ActionResult Json()
    {
        return ActionResult("MyJsonControl", MyJsonString);
    }

But I'd really look into the former before going with the latter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜