当我在.Net中将DateTime解析为json时,它返回一个字符串(即"\/Date(1249335194272)\/").如何使其返回未包装在字符串中的js date对象构造函数?

// js server code
var dteNow = <%= jsonDateNow %>;


// js rendered code
var dteNow = "\/Date(1249335477787)\/";


// C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;

namespace testing{
    public partial class iTaxPrep : System.Web.UI.Page
    {
        protected string jsonDateNow;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                jsonDateNow = new JavaScriptSerializer().Serialize(DateTime.Now);

            }
        }
    }
}

推荐答案

这似乎有效(感谢Chris S的 idea ).在C#中,执行替换以移除日期对象周围的字符串包装器;

    using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.Script.Serialization;
        using System.Web.Script.Services;
        using System.Web.Services;
        using System.Web.UI.WebControls;

        namespace test
        {
            [ScriptService]
            public partial class testing: System.Web.UI.Page
            {
                protected string strCaseID;
                protected string jsonCase;

                protected void Page_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        strCaseID =Tools.GetQueryObject("id");
                        // get a complex object with dates, string, arrays etc.
                        jsonESHACase = new JavaScriptSerializer().Serialize(objCase.Get(strCaseID ))
                            .Replace("\"\\/Date(", "new Date(").Replace(")\\/\"", ")");
                    }
                }
            }
        }

..在删除引号并添加新前缀之后,这个js现在可以工作了.好极了

testCase= <%= jsonESHACase %>;
    if (testCase) {
        document.write(testCase["ClosingDate"].format("MM dd yyyy"));
    }

Asp.net相关问答推荐

Windows 命令行中的/p:是什么意思

我们可以在一个网页中使用多个表单吗?

无法加载文件或程序集.无效指针(HRESULT 异常:0x80004003 (E_POINTER))

web.config 部分的单独配置文件

'Access-Control-Allow-Origin' 标头包含多个值 '*, *',但只允许一个

返回 HttpResponseMessage 时的 WebAPI Gzip

SqlBulkCopy 超时

对布尔查询字符串参数使用true或1

System.Web.HttpContext 无法识别

学习什么 - Ruby on Rails 或 ASP .NET MVC...鉴于熟悉 ASP .NET

如何将 Web 应用程序项目转换为类库项目

HttpContext.Current.Cache.Insert 和 HttpContext.Current.Cache.Add 有什么区别

如何在新实现的接口或基类之间做出决定?

如何使用 WebRequest 发布数据并从网页获取响应

VS 2010 中缺少 App_Code 文件夹

捕获的异常本身为 null !

会员生成密码 仅限字母数字密码?

在 Visual Studio 2010 中随机禁用编辑 aspx/ascx 文件?

在 ASP.NET MVC 中模拟 User.Identity

静态变量是线程安全的吗? C#