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

  • Home
  • SEARCH
  • 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 9253709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:17:14+00:00 2026-06-18T11:17:14+00:00

In one of the actions, I do something like this public HttpResponseMessage Post([FromBody] Foo

  • 0

In one of the actions, I do something like this

public HttpResponseMessage Post([FromBody] Foo foo)
{
    .....
    .....

    var response = 
          Request.CreateResponse(HttpStatusCode.Accepted, new { Token = "SOME_STRING_TOKEN"});
    return response;
}

and more methods like that return an anonymous type instance and it works well.

Now, I’m writing tests for it. I have

HttpResponseMessage response = _myController.Post(dummyFoo);

HttpResponseMessage has a property called Content and has a ReadAsAsync<T>().

I know that if there was a concrete specific type, I can do

Bar bar = response.Content.ReadAsAsync<Bar>();

but how do I access the anonymous type that’s being returned? Is it possible?

I was hoping to do the following:

dynamic responseContent = response.Content.ReadAsAsync<object>();
string returnedToken = responseContent.Token;

but I got the error that instance of type object does not have the property Token. This happens even though the debugger shows responseContent with one property Token. I understand why that’s happening, but I want to know if there is a way to access the Property.

enter image description here

Thanks

  • 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-18T11:17:16+00:00Added an answer on June 18, 2026 at 11:17 am

    .ReadAsAsync<T> is an asynchronous method, meaning that it doesn’t return the whole deserialized object but a Task<T> to handle the continuation of the whole asynchronous task.

    You’ve two options:

    1. Async pattern.

    Use the async keyword in your enclousing method (for example: public async void A()) and do the asynchronous call this way:

    dynamic responseContent = await response.Content.ReadAsAsync<object>();
    string returnedToken = responseContent.Token;
    

    2. Regular task API

    Or just use the Task API:

    response.Content.ReadAsAsync<object>().ContinueWith(task => {
       // The Task.Result property holds the whole deserialized object
       string returnedToken = ((dynamic)task.Result).Token;
    });
    

    It’s up to you!

    Update

    Before you posted the whole screenshot, no one could know that you’re calling task.Wait in order to wait for the async result. But I’m going to maintain my answer because it may help further visitors 🙂

    As I suggested in a comment to my own answer, you should try deserializing to ExpandoObject. ASP.NET WebAPI uses JSON.NET as its underlying JSON serializer. That is, it can handle anonymous JavaScript object deserialization to expando objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have a controller which looks something like this: public class MyController :
I want to have one callback function after actions are done, I'm trying something
I am building an app which looks something like this. It has a action
I'm building an application whose usage is going to look something like this: application
I have this code: class one{ public $instance; function instance(){ $this->instance = 'instance was
I have a simple question about .net delegates. Say I have something like this:
I have a simple question. I have a model that looks like this: public
In a View I send the model encoded like this: var model = '@Html.Raw(Json.Encode(Model))';
I'm trying to access a SOAP service I don't control. One of the actions
in one of my controller actions, I am assigning a project to a user,

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.