Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Json结果之前输出变量名_C#_Asp.net Mvc_Asp.net Mvc 3_Localization_Jsonp - Fatal编程技术网

C# 在Json结果之前输出变量名

C# 在Json结果之前输出变量名,c#,asp.net-mvc,asp.net-mvc-3,localization,jsonp,C#,Asp.net Mvc,Asp.net Mvc 3,Localization,Jsonp,我将JS本地化资源中的本地化键/值对输出到lang.JS中,如下所示: [Route("js/lang.js")] public ActionResult Lang() { ResourceManager manager = new ResourceManager("Normandy.App_GlobalResources.JsLocalization", System.Reflection.Assembly.GetExecutingAssembly())

我将JS本地化资源中的本地化键/值对输出到
lang.JS
中,如下所示:

    [Route("js/lang.js")]
    public ActionResult Lang()
    {
        ResourceManager manager = new ResourceManager("Normandy.App_GlobalResources.JsLocalization", System.Reflection.Assembly.GetExecutingAssembly());
        ResourceSet resources = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true);

        Dictionary<string, string> result = new Dictionary<string, string>();

        IDictionaryEnumerator enumerator = resources.GetEnumerator();

        while (enumerator.MoveNext())
            result.Add((string)enumerator.Key, (string)enumerator.Value);

        return Json(result);
    }
有没有办法让它们成为:

var LANG = {"Test":"test","_Lang":"en"}

看起来您想要返回一个脚本,而不是json对象。在这种情况下,我会做两件事之一

  • 返回封装脚本而不是json的操作结果(不确定是否存在)
  • 返回json,然后将该json设置为客户端上的局部变量(典型的ajax调用)

  • 您可以使用JSONP。编写自定义操作结果:

    public class JsonpResult: ActionResult
    {
        public readonly object _model;
        public readonly string _callback;
    
        public JsonpResult(object model, string callback)
        {
            _model = model;
            _callback = callback;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var js = new JavaScriptSerializer();
            var jsonp = string.Format(
                "{0}({1})", 
                _callback, 
                js.Serialize(_model)
            );
            var response = context.HttpContext.Response;
            response.ContentType = "application/json";
            response.Write(jsonp);
        }
    }
    
    然后在控制器操作中返回:

    [Route("js/lang.js")]
    public ActionResult Lang()
    {
        ...
        return new JsonpResult(result, "cb");
    }
    
    最后定义回调以捕获json,然后再包含脚本:

    <script type="text/javascript">
    function cb(json) {
        // the json argument will represent the JSON data
        // {"Test":"test","_Lang":"en"}
        // so here you could assign it to a global variable
        // or do something else with it
    }
    </script>
    <script type="text/javascript" src="js/lang.js"></script>
    
    
    函数cb(json){
    //json参数将表示json数据
    //{“Test”:“Test”,“_Lang”:“en”}
    //所以这里你可以把它分配给一个全局变量
    //或者用它做点别的
    }
    
    考虑中包含的一些建议,世卫组织已经提供了下面的答案,这些建议非常有趣:
    <script type="text/javascript">
    function cb(json) {
        // the json argument will represent the JSON data
        // {"Test":"test","_Lang":"en"}
        // so here you could assign it to a global variable
        // or do something else with it
    }
    </script>
    <script type="text/javascript" src="js/lang.js"></script>