简体   繁体   中英

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

I'm currently working on a project where WebApi escaping dates. 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. 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:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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