【问题标题】:Webmethod can't remove Object { d: "" }Webmethod 无法移除 Object { d: "" }
【发布时间】:2013-12-30 02:48:17
【问题描述】:

我想做的不是发送Object { d : "{"FileID":"1213"}" }而是发送"{"FileID":"1213"}"

我当前的代码:

using System;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

[ScriptService]
partial class testing_class : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["FileID"] = Request.QueryString["FileID"];

    }

    public static string returnJSON(object o)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Serialize(o);
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
    public static string CurrentFile()
    {
        var d = new { FileID = "123" };
        return returnJSON(d);
    }


};

【问题讨论】:

    标签: c# asp.net json webmethod


    【解决方案1】:

    Microsoft stack Json 序列化程序已经过时,应该不惜一切代价避免使用。相反,您应该使用 Json.NET 实现(并且默认情况下,较新的 .NET Web 堆栈已经是)。

    如果您没有安装它,您可以通过在 NuGet 控制台窗口中运行 Install-Package Newtonsoft.Json 来安装它。还要确保你是using Newtonsoft.Json;

    [WebMethod]
    [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
    public static string CurrentFile()
    {
        var d = new { FileID = "123" };
        return JsonConvert.SerializeObject(d);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多