简体   繁体   English

为什么ASP.net WebApi在JSON输出中转义日期?

[英]Why is ASP.net WebApi escaping dates in JSON output?

I'm currently working on a project where WebApi escaping dates. 我目前正在研究WebApi转义日期的项目。 For example. 例如。

{"Date":"11\/05\/2016"}

and it must be this 而且一定是这个

{"Date":"11/05/2016"}

The model. 该模型。

public class XModel
{
        public string Date { get; set; }
}

The controller. 控制器。

public class DatesController : ApiController
{
    [HttpGet]
    [Route("api/dates/{take:int?}")]
    [ResponseType(typeof(XModel))]
    public XModel GetDates(int take = 0)
    {
        return new XModel()
        {
            Date = "11/05/2016"
        };
    }
}

It looks like the project is escaping every slash ("/") in the JSON response. 看起来该项目正在转义JSON响应中的每个斜杠(“ /”)。 Does anyone know a solution? 有人知道解决方案吗?

Thanks a lot, Jordy 非常感谢,乔迪

I suppose you're already using a json serializer for you web api, so try this way: 我想您已经为您的Web API使用了json序列化程序,因此请尝试以下方式:

public class DatesController : ApiController
{
    [HttpGet]
    [Route("api/dates/{take:int?}")]
    public IHttpActionResult GetDates(int take = 0)
    {
        return Ok(new
        {
            Date = "11/05/2016"
        });
    }
}

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM