• Asp.net mvc 3 JSONResult return array


    publicclassArrayJsonResult:System.Web.Mvc.JsonResult
    {
       
    publicoverridevoidExecuteResult(ControllerContext context)
       
    {
           
    if(context ==null)
           
    {
               
    thrownewArgumentNullException("context");
           
    }
           
    if(JsonRequestBehavior==JsonRequestBehavior.DenyGet&&
               
    String.Equals(context.HttpContext.Request.HttpMethod,"GET",StringComparison.OrdinalIgnoreCase))
           
    {
               
    thrownewInvalidOperationException("JsonRequest_GetNotAllowed");
           
    }

           
    HttpResponseBase response = context.HttpContext.Response;

           
    if(!String.IsNullOrEmpty(ContentType))
           
    {
                response
    .ContentType=ContentType;
           
    }
           
    else
           
    {
                response
    .ContentType="application/json";
           
    }
           
    if(ContentEncoding!=null)
           
    {
                response
    .ContentEncoding=ContentEncoding;
           
    }
           
    if(Data!=null)
           
    {
               
    StringWriter sw =newStringWriter();
                sw
    .Write("[");
               
    try
               
    {
                   
    var collection =DataasIEnumerable<String>;
                   
    int countLessOne = collection.Count()-1;
                   
    for(int i =0; i < countLessOne; i++)
                   
    {
                        sw
    .Write(collection.ElementAt(i));
                        sw
    .Write(",");
                   
    }
                    sw
    .Write(collection.ElementAt(countLessOne));
               
    }
               
    catch(Exception)
               
    {
                   
    //data was not a collection
               
    }

                sw
    .Write("]");
                response
    .Write(sw.ToString());
           
    }
       
    }
  • 相关阅读:
    8 shell五大运算
    android闹钟——原代码【转】
    draw9patch超详细教程【转】
    史上最全的动画效果 Android Animation 总汇 【转】
    Android 中的BroadCastReceiver【转】
    android屏幕适配【转】
    [Android实例] ViewPager多页面滑动切换以及动画效果【转】
    人脸数据库汇总 【转】
    Android闹钟程序周期循环提醒源码(AlarmManager)【转】
    android背景图片更换——经典例子【转】
  • 原文地址:https://www.cnblogs.com/webglcn/p/2661444.html
Copyright © 2020-2023  润新知