Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:10:10+00:00 2026-06-04T10:10:10+00:00

I have an ASP.NET WebApi project that utilizes Entity Framework 4.1. I am attempting

  • 0

I have an ASP.NET WebApi project that utilizes Entity Framework 4.1. I am attempting to return an entity and leveraging JSON.net for deserialization. All is working well however when attempting to deserialize entities with TimeSpan properties I run accross the following exception:

Could not cast or convert from System.String to System.TimeSpan.

Looking at the stack trace reveals:

JsonSerializationException: Error converting value
“PT17H9M43.1671969S” to type ‘System.TimeSpan’. Path ‘TimeIn’, line 1,
position 890.

The output from response.Content.ReadAsStringAsync().Result is:

{“CreatedDate”:”/Date(1329851383157-0500)/”,”ServicerUserId”:6,”TimeIn”:”PT17H9M43.1671969S”,”TimeOut”:”PT17H28M43.1671969S”}

Before using JSON.net I have successfully deserialized using System.Runtime.Serialization.JsonDataContractJsonSerializer however would prefer to not use this approach.

Code used to get entity from WebApi

public T Get<T>(object id)
{
    using (var httpClient = NewHttpClient())
    {
        var response = httpClient.GetAsync(_endpoint + id.ToString()).Result;
        return JsonConvert.DeserializeObject<T>(response.Content.ReadAsStringAsync().Result);
    }
}

Code used to in WebApi controller to return entity:

// GET /api/services/5
public Service Get(Guid id)
{
    var entry = db.Services.Find(id);
    if (entry == null)
        throw new HttpResponseException(HttpStatusCode.NotFound);
    return entry;
}

Any ideas on how to deserialize TimeSpan’s would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:10:12+00:00Added an answer on June 4, 2026 at 10:10 am

    The solution was in configuring the WebApi project to leverage JSON.net for serialization by default in the global.asax Application_Start event as referenced at http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx

     // Create Json.Net formatter serializing DateTime using the ISO 8601 format
                JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
                serializerSettings.Converters.Add(new IsoDateTimeConverter());
                GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is that I have a asp.net solution that uses a seperate project
I have ASP.NET project and attached mdf database.I want to deploy it to remote
I have asp.net form that contains fields. When I access this window, my javascript
I have an Asp.net app that simply reads an xml file and this code
The project is developed using ASP.NET MVC framework and heavily relies on .NET 3.5.
Let's say I have an ASP.NET MVC webapp that calls a repository layer, which
I am going lead a project, where I have Asp.Net webapp with SQL Server
I have a DLL that provides mail sending functionality to an asp.net application, this
Suppose I have a ASP.NET Web API controller that uses a service, which in
OK, this begins to drive me crazy. I have an asp.net webapp. Pretty straightforward,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.